获取数组中值的百分比(Java)

时间:2015-04-09 20:28:30

标签: java arrays

对于作业,用户输入主队8分,对手队8分。作业的公园要求输出显示主队的比赛数量以及主队整个赛季的胜率。 代码如下: 顺便说一下,分配所需的步骤和方法还有很多,这些步骤和方法都很完美,只有这一步/方法不是。

public static void main (String [] args){
    final int tGame = 8;
    final int oGame = 8;
    int [] texansScore;
    int [] opponentsScore;;
    texansScore = new int [8];
    opponentsScore = new int [8];
    Scanner sc = new Scanner(System.in);
    for (int t = 0; t < 8; t++){
        System.out.println("Score for Game " + (t + 1) + ": ");
        System.out.println(" Please enter in the Houston Texans'score: ");
        texansScore [t] = sc.nextInt();
        System.out.println(" Please enter in the opponents' score: ");
        opponentsScore [t] = sc.nextInt();


        }
        dterminePercent(texansScore, opponentsScore);
}
public static double dterminePercent ( int [] tGame, int [] oppGame){
        int [] array = new int [tGame.length];
        double won = 0;
        double winPercent = 0;
        for (int i =0; i < tGame.length; i++){
            if ( tGame [i] > oppGame [i]){
            array [i] = tGame[i];
            won = i + 1;
        }
            winPercent = won / 8.0 * 100.0;
        }
        System.out.println("The amount of games won is: " + won + "\nThe win perecntage is: " + winPercent + "%");
        return winPercent;
    }

1 个答案:

答案 0 :(得分:0)

won = i + 1;更改为won ++;。您希望增加won,而不是使其高于循环索引i