变量未显示在system.out.println中

时间:2018-09-08 17:28:08

标签: java variables calculator

我编写了一个程序,该程序接受考试成绩,并将变量分配给某个星级和字母等级。该程序正在编译,但是在我认为的变量中有2个问题。我无法获得最低的分数,也无法获得最高的分数。这两个变量似乎都以正确的方式定义,因此我希望有人可以提供帮助。

public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.print("Welcome to GradeCalculator!\n\n"); // Start of the program

            // All variables are defined below
    int students = 0;
    int exams = 0;
    double classav = 0;
    int count = 0;
    double lowest = 0;
    double highest = 0;
    String name;

            // Intake and output portions of the program for number of students, and number of exams 
    System.out.print("Please enter the number of students: ");
    students = s.nextInt();
    System.out.print("Please enter the number of exams: ");
    exams = s.nextInt();
    System.out.println("\n----------------------------------------");

    String[] examScores = new String [exams];

    while(count < students) {
        int totalExam=0;
        int starnum=0;
        char letter='a';
        String stars= "";

    //New Scanner Statement to fix String "name" problems   
    Scanner n = new Scanner(System.in);

    System.out.print("Enter student " + (count+1) +"'s name\t: ");
    name = n.nextLine();
    n.nextLine(); 
    System.out.print("Enter exam scores\t: ");
    examScores = n.nextLine().split(" ");

    for(int i = 0; i < exams; i++){
        totalExam += Integer.parseInt(examScores[i]);
    }

            //for loop used to compare exam grades. printing out proper star counts according to letter grades within the if statements
    int studentavg = totalExam / exams;
    System.out.println(" Average: " + studentavg);

    if(studentavg>=90){
        letter = 'A';
        stars = "****";
        starnum = 4;
    }//if1
    if(studentavg<90 && studentavg>=80){
        letter = 'B';
        stars = "***";
        starnum = 3;
    }//if2
    if(studentavg<80 && studentavg>=70){
        letter = 'C';
        stars = "**";
        starnum = 2;
    }//if3
    if(studentavg<70 && studentavg>=60){
        letter = 'D';
        stars = "*";
        starnum = 1;
    }//if4
    if(studentavg<60){
        letter = 'F';
        stars = "";
        starnum = 0;
    }//if5

    System.out.println("Grade statistics for " + name);
    System.out.println(" Letter Grade: " + letter);
    System.out.println(name + " gets " + starnum + " stars! "+ stars);


    System.out.println("\n----------------------------------------");
    classav += studentavg;
    count++;
    }// while loop




    System.out.println("Class statistics: ");
    System.out.println(" Average: "+ (classav / exams));
    System.out.println(" Lowest : " + lowest);
    System.out.println(" Highest: " + highest);
    System.out.println("Done. Good bye!");

    }
}

现在的结果     ----------------------------------------

Welcome to GradeCalculator!

Please enter the number of students: 2
Please enter the number of exams: 3

----------------------------------------
Enter student 1's name  : Rob
Enter exam scores   : 100 99 77
 Average: 92
 Grade statistics for Rob
 Letter Grade: A
 Rob gets 4 stars! ****

----------------------------------------
Enter student 2's name  : Tom

Enter exam scores   : 100 99 77
 Average: 92
Grade statistics for Tom
 Letter Grade: A
Tom gets 4 stars! ****

----------------------------------------
Class statistics: 
Average: 30.666666666666668
Lowest : 0.0
Highest: 0.0
Thank you for using Rob Nelson's Grade Calculator! Good bye!  :)
BUILD SUCCESSFUL (total time: 56 seconds)

预期结果     ----------------------------------------

Welcome to GradeCalculator!

Please enter the number of students: 2
Please enter the number of exams: 3

----------------------------------------
Enter student 1's name  : Rob
Enter exam scores   : 100 99 77
 Average: 92
 Grade statistics for Rob
 Letter Grade: A
 Rob gets 4 stars! ****

----------------------------------------
Enter student 2's name  : Tom

Enter exam scores   : 100 99 77
 Average: 92
Grade statistics for Tom
 Letter Grade: A
Tom gets 4 stars! ****

----------------------------------------
Class statistics: 
Average: 92
Lowest : 77
Highest: 100
Thank you for using Rob Nelson's Grade Calculator! Good bye!  :)
BUILD SUCCESSFUL (total time: 56 seconds)

1 个答案:

答案 0 :(得分:0)

您的问题之一是您将Scanner s用于2种不同类型的变量,因此,当您使用Scanner作为字符串值(例如:name = s.nextLine();)时,您将需要制作一个新的Scanner语句(例如:Scanner n = new Scanner(System.in);然后您将s.nextLine替换为n.nextLine。解决此问题并不能完全解决您代码中的问题。我将对其进行更多地研究和更新您是否有其他错误的解决方法