无效输入错误和成绩计算器的协作类统计所需的指导

时间:2014-10-12 11:59:27

标签: java

我正在使用Java构建一个成绩计算器,我在添加一些功能时遇到了麻烦,而且当我尝试进行更改时,我似乎仍在继续使用它。我整个星期都在研究它,并在书中和powerpoint幻灯片中重新开始,我觉得我只有一些片段仍然没有得到。

我需要确保无效分数,每次输入负分数时都会显示重新输入错误。然后我需要计算平均,最低和最高分的等级统计。所以基本上是输入了多少数据的合作,可以是任何数量的考试或学生。

这是我的代码,如果您需要更多信息,请告诉我。我真的很陌生,所以我很抱歉这不是最好的。

import java.util.Scanner; 

public class GradeCalculator {

public static void main(String[] args){

    double examAverage = 0, scoresEntered = 0, examSum = 0;  
    double totalExamSum = 0, allScoresEntered = 0;
    //variables for input

    Scanner GC = new Scanner(System.in);
    //Scanner for integer inputs

    System.out.println("Welcome to Grade Calculator!" +"\n");

    System.out.println("Please enter the number of students:");
        int numberStudents = GC.nextInt();  
        //number of students input

    System.out.println("Please enter the number of exams:");
        int numberOfExams = GC.nextInt();
        //number of exams input

        for (int i = 1; i <= numberStudents; i++)  { 
            Scanner name = new Scanner(System.in);                
            //scanner for student name input
            //Scanner for name input
            System.out.println("\n--------------------------------------");

            System.out.print("Enter student " + i + "'s name : " );
            String studentname = name.nextLine();
            //student name input

            System.out.print("Enter exam scores      : ");

        for (int j = 0; j < numberOfExams; j++) {
             scoresEntered = GC.nextDouble();
             examSum = (examSum + scoresEntered);}
             //score input and sum of all input scores
        do{
              System.out.println("Invalid exam scores, reenter: ");
              scoresEntered =GC.nextDouble();
       } while(scoresEntered<0);
        //my attempt at the Invalid exam score error

        examAverage = (examSum/numberOfExams);
        //examaverage calculator

        System.out.println("\n--------------------------------------");

             System.out.println("Grade Statistics for " + name);
             System.out.println("  Average   : " + examAverage);

    //Conditions and print outputs below for grade averages  
    if(examAverage <= 100 & examAverage >=90){
        System.out.println("  Letter Grade:  A");
        System.out.println("  " + name + " gets 4 stars! ****");
        examAverage = 0;
        examSum = 0;}

        else if(examAverage <=89.99 & examAverage >=80){
            System.out.println("  Letter Grade:  B");
            System.out.println("  " + name + " " + " gets 3 stars! ***");
            examAverage = 0;
            examSum = 0;}

        else if(examAverage <=79.99 & examAverage >=70){
            System.out.println("  Letter Grade:  C");
            System.out.println("  " + name + " " + " gets 2 stars! **");
            examAverage = 0;
            examSum = 0;}

        else if(examAverage <=69.99 & examAverage >=60){
            System.out.println("  Letter Grade:  D");
            System.out.println("  " + name + " " + " gets 1 stars! *");
            examAverage = 0;
            examSum = 0;}

        else if(examAverage <=59.99 & examAverage >=50){
            System.out.println("  Letter Grade:  F");
            System.out.println("  " + name + " " + " gets 0 stars!");
            examAverage = 0;
            examSum = 0;}

    //still need class statistics as well as help with the invalid exam scores, reenter error.
            }
        }
}

1 个答案:

答案 0 :(得分:0)

向我跳出来的是围绕你的分数输入:

for (int j = 0; j < numberOfExams; j++)
{
  scoresEntered = GC.nextDouble();
  examSum = (examSum + scoresEntered);
}

//score input and sum of all input scores
do
{
  System.out.println("Invalid exam score, reenter: ");
  scoresEntered = GC.nextDouble();
} while(scoresEntered < 0);

我故意重新格式化你发布的内容,试图让问题更加明显。你似乎正在做的是读取所有分数。然后,无论输入的是什么,您都会告诉用户考试成绩无效并要求他们重新输入。记住,do ... while循环总是至少执行一次。如果第一次重新输入的分数高于零,那就是你读完所有内容,然后满足。

如果您在将每个分数添加到examSum之前尝试对其进行验证,则需要更多类似的内容:

for (int j = 0; j < numberOfExams; j++)
{
  while ((scoresEntered = GC.nextDouble()) < 0)
    System.out.println("Invalid exam scores, reenter:  ");

  examSum = (examSum + scoresEntered);
}