簡體   English   中英

我如何增加數組

[英]How can I increment the Array

我不確定我要問的內容是否正確,但最初我主要運行了5次。 但是我覺得一個do / while循環可以做同樣的事情。 但是現在我無法將數組shotsMade從shotsMade [0]更改為shotsMade [1]等,也無法將shotCount存儲到其中。 它只會存儲while循環的最后一次運行。 我可以更改使這兩項增加的方法,以便該方法仍能正確計算數據

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;
    }

}

兩件事情:

  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產生正確的索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM