如何在运行代码时实现-1

时间:2015-02-26 18:00:45

标签: java

代码工作正常,但我似乎无法运行代码,-1是我用来计算平均值的唯一输入。

import java.util.*;

class Bowling {

    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);
        int G = 0;

        int scores = 0;
        double avg = 0;
        double highest_average = 0;
        int times = 0;
        int players = 4;

        for (int i = 1; i <= players; i++) {
            System.out.println("Player " + i);
            if (avg > highest_average)
                highest_average = avg;
            do {
                System.out.println("Enter your bowling score:");
                G = keyboard.nextInt();
                scores += G;
                times++;
            }
            while ((G != -1));
            scores += +1;
            times = times - 1;
            avg = (scores) / (times);
            System.out.printf("Average= %.2f\n", avg);
            scores = 0;
            times = 0;
            System.out.println("");
        }

        System.out.println("Highest average:" + highest_average);
    }
}

这是一个示例运行:所以代码工作正常,但它只是我不知道我是否应该使用IF语句让-1计算为平均值。

Player 1

Enter your bowling score:

300

Enter your bowling score:

222

Enter your bowling score:

-1

Average= 261.00

Player 2

Enter your bowling score:

210

Enter your bowling score:

211

Enter your bowling score:

300

Enter your bowling score:

-1

Average= 240.00

Player 3

Enter your bowling score:

233

Enter your bowling score:

-1

Average= 233.00

Player 4

Enter your bowling score:

112

Enter your bowling score:

-1

Average= 112.00

Highest average:261.00

这是另一次运行但是先输入-1

Player 1

Enter your bowling score:

-1 

Exception in thread "main" java.lang.ArithmeticException: / by zero
at LabQuiz1.main(LabQuiz1.java:33)    // I keep getting this when -1 is inputted I have been thinking of using a IF statement to tell the code that if  -1 is implemented it should still calculate 0 as the average. 

2 个答案:

答案 0 :(得分:1)

您的代码在这里:

scores += + 1;//change to scores+=1
times = times - 1; //in do while loop you increment by one and now you decrement it and hence 0
avg = (scores)/(times);//you could check for times if 0 here. and you are dividing by integer if you want accurate decimal result change denominator to decimal as well.

将其更改为:

scores += 1;
times = times - 1; 
avg = times == 0? 0: (scores/(times * 1.));

答案 1 :(得分:1)

如果您使用-1作为特殊数字来退出处理,那么您应该使用 if 条件来处理输入作为计算的一部分。

double highest_average=0;
int players = 4;

for(int i =1; i<=players; i++) {
  double avg = 0;
  int times = 0;
  int scores = 0;

  System.out.println("Player " + i);
  if (avg > highest_average) 
    highest_average= avg; 

  do
  {
    System.out.println("Enter your bowling score:");
    G = keyboard.nextInt();
    if(G ==-1)
      break;

    scores += G;
    times++;
  }
  while(true);   

  // remove the following lines that aren't necessary anymore
  //scores += + 1;
  //times = times - 1; 

  if(times > 0)
    avg = (scores)/(times);

  System.out.printf("Average= %.2f\n", avg);
  //scores =0; times =0; 
  System.out.println("");
}

如果您不想允许负数,请相应更改 if 条件。

您可以修改执行/执行条件以执行... while(true),因为现在您退出 break 语句中的循环。

您可以将平均得分变量移到 for 循环中,因为它们是仅在for循环的范围内使用。这些变量初始化为0,而不是需要在底部再次初始化值。