2014-03-28 32 views
0

我不確定我所問的是否正確,但最初我已經在主體中運行了5次。但是我覺得do/while循環可以做同樣的事情。但是現在我無法獲得陣列鏡頭從shotMade [0]改變爲shotsMade [1]等等,以及shotCount來存儲它。它只會存儲while循環的最後一次運行。我能更改使這些2項增加這樣的方法仍然正確我該如何增加數組

import java.util.*; 

public class Final { 
public static void main(String[] args) { 
    int myGameCounter = 1; 
    int [] shotsMade = new int [5]; 
    System.out.print("Enter Player's Free Throw Percentage: "); 
    Scanner input = new Scanner(System.in); 
    int percent = input.nextInt(); 


    //Game 
do{ 
    System.out.println("Game " + myGameCounter + ":"); 
    Random r = new Random(); 
    myGameCounter++; 
    int shotCount = 0; 
    for (int i = 0; i < 10; ++i){ 
     boolean in = tryFreeThrow(percent); 
     if (in) { 
     shotCount++; 
     System.out.print("In" + " "); 
     } 
     else { 
     System.out.print("Out" + " "); 
     } 
    } 
    System.out.println(""); 
    System.out.println("Free throws made: " + shotCount + " out of 10"); 
    System.out.println(""); 
    shotsMade[0]= shotCount;// I need shotsMade[0] to change each loop, shotsMade[1], shotsMade[2], shotsMade[3], shotsMade[4] 
} while (myGameCounter <=5); 

    System.out.println(""); 
    System.out.println("Summary:"); 
    System.out.println("Best game free throws made: " + max(shotsMade)); 
    System.out.println("Worst game free throws made: " + min(shotsMade)); 
    System.out.println("Total Free Throws Made: " + sum(shotsMade) + " " + "out of 50"); 
    System.out.println("Average Free Throw Percentage: " + average(shotsMade) +"%");  


    }  
    public static boolean tryFreeThrow(int percent) { 
    Random r = new Random(); 
    int number = r.nextInt(100); 
    if (number > percent){ 
     return false; 
    } 
    return true; 
    } 
    public static int average (int nums[]) { 
    int total = 0; 
    for (int i=0; i<nums.length; i++) { 
     total = total + nums[i]; 
    } 
    int average = total*10/nums.length; 
    return average; 
    } 
    public static int sum(int nums[]) { 
    int sum = 0; 
    for (int i=0; i<nums.length; ++i) { 
     sum += nums[i]; 
    } 
    return (int)sum; 
    } 
    public static int max(int nums[]) { 
    int max = nums[0]; 
    for (int i=1; i<nums.length; i++) { 
     if (nums[i] > max) 
      max = nums[i]; 
    } 
    return max; 
    } 
    public static int min(int nums[]) { 
    int min = nums[0]; 
    for (int i=1; i<nums.length; i++) { 
     if (nums[i] < min) 
      min = nums[i]; 
    } 
    return min; 
    } 

} 
+0

'shotsMade [0] = shotCount;' – nattyddubbs

+0

更改'shotsMade [0] = shotCount;'使用變量而不是0. – azurefrog

+0

這還不清楚。也許'shotsMade [myGameCounter - 1] = shotCount;'? – 2014-03-28 19:11:54

回答

0

兩件事計算數據:

  1. 移動myGameCounter++;僅低於你設置shotsMade[]

    否則,在第一場比賽結束前將比賽數量提高到2。 它應該看起來像:

    shotsMade[myGameCounter-1]= shotCount; myGameCounter++; } while (myGameCounter <=5);

  2. 設置shotsMade[myGameCounter-1]= shotCount;,而不是shotsMade[0]= shotCount;

    否則,你是,你是重新使用覆蓋的shotsMade[0]

這樣的價值計數器作爲數組的索引。由於myGameCounter在每場比賽後將增加1(在您更改點1之後)並從1開始,因此使用myGameCounter - 1將爲您的陣列產生正確的索引shotsMade

+0

真棒,謝謝,工作。沒有意識到,它可以在存儲價值之前改變遊戲數量。 – planker1010