2013-03-07 34 views
0

我是一个完整的Newb与Java和我有一个非常好奇的问题发生在我的代码。该对象是创建一个数组中20个随机数的列表,以显示该数组并根据数组中的数据执行一系列计算。这必须通过6种方法来完成。除了最后一种方法,一切似乎都很好。这意味着要获取数组中的最小数字,但由于某种原因,它只能输出0.奇怪的是,对于上面收集数组中最高数字的方法而言,这是完美的。java低价值输出0

我试图麻烦拍摄它,看看它是否可能来自具有空值的数组,但这不太可能,因为最高的方法似乎工作正常。最低列表靠近底部。

谢谢你的帮助,非常感谢这个新手。此外,如果你有任何其他的评论会很棒,我很乐意接受批评!

import java.util.Random; 

public class Program7 { 

    public static void main(String[] args) { 

     final int listSize = 20; // List size 
     double total = 0;   // Total 
     int[] ranList = new int[listSize]; // Array with random numbers 
     int highest = ranList[0]; // Highest variable 
     int lowest = ranList[0]; // Lowest Variable 

     randomNumberList(ranList);   // Calls randomNumberList method 
     displayList(ranList);    // Calls displayList method 
     total = totalList(total, ranList); // Calls totalList method 
     double averageList = listAverage(total, ranList); // Calls averageList method 
     highest = listHighest(ranList, highest); // Calls listHighest method 
     lowest = lowestList(ranList, lowest); // Calls lowestList method 

     // On screen output 
     System.out.println("\nHere's the total: " + total); 
     System.out.println("Here's the average: " + averageList); 
     System.out.println("Here's the highest number in the list: " + highest); 
     System.out.println("Here's the lowest number in the list: " + lowest); 

    } 

    /** 
    * Generates a random list 
    * 
    * @param ranList 
    */ 
    public static void randomNumberList(int[] ranList) { 
     for (int i = 0; i < ranList.length; i++) { 
      Random generator = new Random(); 
      int ranNum = generator.nextInt(100) + 1; 
      ranList[i] = ranNum; 
     } 
    } 

    /** 
    * Displays list 
    * 
    * @param ranList 
    */ 
    public static void displayList(int[] ranList) { 
     System.out.println("Here is your random list: "); 
     for (int i = 0; i < ranList.length; i++) { 
      System.out.print(ranList[i] + " "); 
     } 
    } 

    /** 
    * Adds elements in list 
    * 
    * @param total 
    * @param ranList 
    * @return returns total of list added together 
    */ 
    public static double totalList(double total, int[] ranList) { 
     for (int i = 0; i < ranList.length; i++) { 
      total += ranList[i]; 
     } 
     return total; 
    } 

    /** 
    * Finds average by dividing the total with the ranList.length 
    * 
    * @param total 
    * @param ranList 
    * @return result of the averaging 
    */ 
    public static double listAverage(double total, int[] ranList) { 
     double averageList = total/ranList.length; 
     return averageList; 
    } 

    /** 
    * Steps through array via loop and finds highest value 
    * 
    * @param ranList 
    * @param highest 
    * @return the highest value 
    */ 
    public static int listHighest(int[] ranList, int highest) { 
     for (int i = 0; i < ranList.length; i++) { 
      if (ranList[i] > highest) { 
       highest = ranList[i]; 
      } 
     } 
     return highest; 
    } 

    /** 
    * Steps through array via loop and finds lowest value 
    * 
    * @param ranList 
    * @param lowest 
    * @return the lowest value 
    */ 
    public static int lowestList(int[] ranList, int lowest) { 
     for (int i = 0; i < ranList.length; i++) { 
      if (ranList[i] < lowest) { 
       lowest = ranList[i]; 
      } 
     } 
     return lowest; 
    } 
} 

回答

0

您应该在创建随机数组后初始化最高和最低值。它们都从0开始。

1

在将它传入方法之前,您将最低位设置为0。

int[] ranList = new int[listSize]; // Array with random numbers (not yet, right now this is an array of all 0) 
int highest = ranList[0]; // Highest variable (not really, this is 0) 
int lowest = ranList[0]; // Since you didn't put anything in ranList, lowest is now 0 

为了您的最高功能,因为你的随机数均大于0

真的,这里的根本原因是,你不应该通过任何东西到这些方法在所有这一切工作正常,因为他们没有做任何事情。

public static int lowestList(int[] ranList) { 
    int lowest = Integer.MAX_VALUE; 
    for (int i = 0; i < ranList.length; i++){ 
     if (ranList[i] < lowest){ 
      lowest = ranList[i]; 
     } 
    } 
    return lowest; 
} 
+0

这个解决方案震撼我的袜子,谢谢! – JavaTheMutt 2013-03-07 20:24:59

0

尽量把

lowest = ranList[0]; 

您生成列表后,即调用后

randomNumberList(ranList);