猿问

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

当我输入一个有非法分数提示的数字时,我的问题出现了,它仍然向我的计数器添加一个数字,并将该数字添加到总数中,因此平均值被丢弃。我一直在绞尽脑汁,尝试了几乎所有方法来弄清楚。代码有点草率,因为我还没有清理它,我只是想先整理一下移动部分。


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


芜湖不芜
浏览 148回答 2
2回答

POPMUISE

首先检查分数是否合法,然后增加计数器并将其添加到您的total. 您还可以分配score和检查不是-1通过单个操作。并且始终使用大括号(即使它们是可选的)。喜欢,// read the first scoreSystem.out.printf("Enter a score&nbsp; (1-100, -1 to quit)" + ": ", scoreCount);while ((score = stdin.nextDouble()) != -1.0) {&nbsp; &nbsp; if (score < -1 || score > 100) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Illegal score.&nbsp; Try again");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; scoreCount++;&nbsp; &nbsp; &nbsp; &nbsp; total += score;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.printf("Enter a score&nbsp; (1-100, -1 to quit)" + ": ", scoreCount);}

至尊宝的传说

试试这个代码。这个对我有用:public class StudentSentinel&nbsp; {&nbsp;public static void main(String[] args) {&nbsp; &nbsp; double score;&nbsp; &nbsp; double total = 0.0;&nbsp; &nbsp; double average;&nbsp; &nbsp; int scoreCount = 0;&nbsp; &nbsp; // create the Scanner object. Name it stdin&nbsp; &nbsp; Scanner stdin = new Scanner(System.in);&nbsp; &nbsp; // title at the top of the output&nbsp; &nbsp; System.out.println(" student score report");;&nbsp; &nbsp; //&nbsp; &nbsp;read the first score&nbsp; &nbsp; System.out.printf("Enter a score&nbsp; (1-100, -1 to quit)" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ": ", scoreCount);&nbsp; &nbsp; score = stdin.nextDouble();&nbsp; &nbsp; scoreCount++;&nbsp; &nbsp; total += score;&nbsp; &nbsp; while (score != -1.0) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Enter a score&nbsp; (1-100, -1 to quit)" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ": ", scoreCount);&nbsp; &nbsp; &nbsp; &nbsp; scoreCount++;&nbsp; &nbsp; &nbsp; &nbsp; score = stdin.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; if (score < -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Illegal score.&nbsp; Try again");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }else if (score > 100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Illegal score.&nbsp; Try again");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; total += score;&nbsp; &nbsp; &nbsp; &nbsp; // increment the loop counter&nbsp; &nbsp; }&nbsp; &nbsp; // end of for loop&nbsp; &nbsp; average = total / scoreCount;&nbsp; &nbsp; System.out.printf("\nThe average score for %d students is %8.2f\n",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scoreCount, average);} // end of main}
随时随地看视频慕课网APP

相关分类

Java
我要回答