2013-03-07 63 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);