如何将无效数字添加到我的最终计算和计数器总数中?

问题描述 投票:0回答:2

当我输入一个我有非法分数提示的数字时,我的问题出现了,它仍然在我的计数器上添加一个数字和总数,所以平均值被抛弃了。我一直在绞尽脑汁,几乎尝试了一切来解决它。代码有点草率,因为我还没有把它清理干净,我只是想先把运动部件整理好。

public class StudentSentinel {

  public static void main(String[] args) {
    double score;
    double total = 0.0;
    double average;
    int scoreCount = 0;

    // create the Scanner object. Name it stdin
    Scanner stdin = new Scanner(System.in);

    // title at the top of the output
    System.out.println(" student score report");;

    //   read the first score
    System.out.printf("Enter a score  (1-100, -1 to quit)" +
      ": ", scoreCount);

    score = stdin.nextDouble();
    scoreCount++;

    while (score != -1.0) {

      total += score;
      System.out.printf("Enter a score  (1-100, -1 to quit)" +
        ": ", scoreCount);
      scoreCount++;
      score = stdin.nextDouble();
      if (score < -1)
        System.out.println("Illegal score.  Try again");
      else if (score > 100) {
        System.out.println("Illegal score.  Try again");
      }
      // increment the loop counter
    }
    // end of for loop
    average = total / scoreCount;
    System.out.printf("\nThe average score for %d students is %8.2f\n",
      scoreCount, average);
  } // end of main    
} // end of class definition
java
2个回答
3
投票

首先检查分数是否合法,然后递增计数器并将其添加到total。您也可以通过一次操作分配score并检查它不是-1。并且总是使用大括号(即使它们是可选的)。喜欢,

// read the first score
System.out.printf("Enter a score  (1-100, -1 to quit)" + ": ", scoreCount);
while ((score = stdin.nextDouble()) != -1.0) {
    if (score < -1 || score > 100) {
        System.out.println("Illegal score.  Try again");
    } else {
        scoreCount++;
        total += score;
    }
    System.out.printf("Enter a score  (1-100, -1 to quit)" + ": ", scoreCount);
}

1
投票

试试这个代码吧。这个对我有用:

public class StudentSentinel  {

 public static void main(String[] args) {
    double score;
    double total = 0.0;
    double average;
    int scoreCount = 0;

    // create the Scanner object. Name it stdin
    Scanner stdin = new Scanner(System.in);

    // title at the top of the output
    System.out.println(" student score report");;

    //   read the first score
    System.out.printf("Enter a score  (1-100, -1 to quit)" +
            ": ", scoreCount);

    score = stdin.nextDouble();
    scoreCount++;
    total += score;
    while (score != -1.0) {


        System.out.printf("Enter a score  (1-100, -1 to quit)" +
                ": ", scoreCount);
        scoreCount++;
        score = stdin.nextDouble();
        if (score < -1) {
            System.out.println("Illegal score.  Try again");
            continue;
        }else if (score > 100) {
            System.out.println("Illegal score.  Try again");
            continue;
        }
        total += score;
        // increment the loop counter
    }
    // end of for loop
    average = total / scoreCount;
    System.out.printf("\nThe average score for %d students is %8.2f\n",
            scoreCount, average);
} // end of main

}

© www.soinside.com 2019 - 2024. All rights reserved.