简体   繁体   English

成绩计算器不会循环到第二名学生

[英]Grade Calculator Does Not Loop to Second Student

I saw a recent post that is literally trying to achieve the same thing as me, but his code stops at one student and it won't work. 我看到了最近的一篇文章,该文章实际上试图实现与我相同的目的,但是他的代码仅限于一名学生,并且不起作用。 I managed to fix what was wrong with his code but there are two things that I still don't understand. 我设法解决了他的代码出了什么问题,但是有两件事我仍然不明白。 When I input the student's name with a first and last name the java code has an error and the program stops. 当我输入学生的名字以及姓氏和名字时,java代码出现错误,程序停止运行。 When I use just a first name the program goes all the way through but stops after I calculate one of the student's grades. 当我仅使用名字时,该程序会一直执行,但在我计算出一个学生的成绩后停止。 It does not continually loop until the number of students I've entered have all their grades. 直到我输入的学生人数达到所有成绩后,它才会持续循环播放。

public class proj2
{
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        System.out.println("Welcome to Gradecalculator!");

        System.out.println("Please enter the number of students:");
        int students = s.nextInt();

        System.out.println("Please enter the number of exams:");
        int exams = s.nextInt();

        int i = 0;
        int studentnumber = 1;

        int sum = 0;
        while (i < students) {

            System.out.println("Enter student " + studentnumber++ + "'s name :");
            String studentname = s.next();

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

            for (; i < exams; i++) {
                int n = s.nextInt();
                sum+=n;

                if (n < 0) {
                    System.out.println("Invalid exam scores, reenter: ");
                }
            }
            double average = sum/exams;
            if (average <= 100 && average >= 90) {
                System.out.println("Letter grade: A");
                System.out.println(studentname + " gets 4 stars! ****");
            }
            if (average <= 89 && average >= 80) {
                System.out.println("Letter grade: B");
                System.out.println(studentname + " gets 3 stars! ***");
            }
            if (average <= 79 && average >= 70) {
                System.out.println("Letter grade: C");
                System.out.println(studentname + " gets 2 stars! **");
            }
            if (average <= 69 && average >= 60) {
                System.out.println("Letter grade: D");
                System.out.println(studentname + " gets 1 star! *");
            }
            if (average <= 59) {
                System.out.println("Letter grade: F");
                System.out.println(studentname + " gets 0 stars!");
            }
        }
    }
}

Here is my code, and my objective is to have the program loop again once I've already obtained my output for student one, but it just stops at averaging the grade for the first student. 这是我的代码,我的目标是一旦我已经获得了学生的输出,就再次让程序循环,但是它只是停止了对第一个学生的平均评分。

When I input the student's name with a first and last name the java code has an error and the program stops 当我输入学生的名字以及姓氏和名字时,java代码出现错误,程序停止运行

I'm guessing that the error you're getting is java.util.InputMismatchException . 我猜测您得到的错误是java.util.InputMismatchException This happens because String studentname = s.next(); 发生这种情况是因为String studentname = s.next(); only gets one token, and when you enter a name with two tokens like John Doe , then second token remains in the input buffer and is parsed by the next call int n = s.nextInt(); 仅获得一个令牌,当您输入带有两个令牌的名称(如John Doe ,第二个令牌将保留在输入缓冲区中,并由下一次调用进行解析int n = s.nextInt(); which of course won't work. 哪个当然行不通。 Instead of using String studentname = s.next(); 而不是使用String studentname = s.next(); you want to read the entire line with String studentname = s.nextLine(); 您想使用String studentname = s.nextLine();读取整行String studentname = s.nextLine(); (the best option might be to use Integer.parseInt() for all the calls when you want to read numbers.) (最好的选择可能是在您要读取数字时对所有呼叫使用Integer.parseInt()。)

Also, when you read data using nextInt() the newline character won't be consumed which means that when you go on to the next call it's still there and will be parsed instead of the name (in this case). 同样,当您使用nextInt()读取数据时,换行符将不会被使用,这意味着当您继续进行下一个调用时,它仍然存在并且将被解析而不是名称(在这种情况下)。 Either read an empty line using s.nextLine() before you read the name, or use Integer.parseInt(s.nextLine()) to read the data. 在读取名称之前,请使用s.nextLine()读取空行,或者使用Integer.parseInt(s.nextLine())读取数据。

You might also want to use exceptions to catch errors due to bad input gracefully. 您可能还希望使用异常来优雅地捕获由于错误输入而导致的错误。

On a side note class names in Java should start with a capital letter as stated in the Code Conventions for the Java Programming Language 另外,如Java编程语言代码约定中所述,Java中的类名称应以大写字母开头。

See the documentation for Scanner.nextLine() . 请参阅Scanner.nextLine()的文档。

Also, as noted in another answer and what I missed is that you're using the same variable i in both the while loop that gets student names and the for loop that gets scores for each student. 另外,正如另一个答案中所指出的,我想念的是,您在获取学生姓名的while循环和获取每个学生分数的for循环中都使用了相同的变量i This won't work. 这行不通。 You should use another variable like j in the second loop: 您应该在第二个循环中使用另一个变量,例如j

for (int j = 0; j < exams; j++) {
                int n = s.nextInt();
                sum+=n;

                if (n < 0) {
                    System.out.println("Invalid exam scores, reenter: ");
                }
            }

Ideally you'll want to use am object oriented approach and create student objects with associated test scores and read data into an collection (maybe an arrayList) of students, but maybe that's is a solution at a later stage in the course or exercise path you seem to be on. 理想情况下,您将要使用面向对象的方法,并创建具有相关测试分数的学生对象,并将数据读入学生的集合(可能是arrayList)中,但这也许是您在课程或练习路径的后期阶段的解决方案似乎在。

You are using i as in index to both the outer (students) and inner (exams) loops. 您正在使用i作为外部(学生)循环和内部(考试)循环的索引。 You should use separate variables to ensure that the inner loop doesn't interfere with the outer loop. 您应该使用单独的变量,以确保内部循环不会干扰外部循环。

It looks like you are doing the While loop then the for loop then the if and back to the for loop till it finishes all the grades. 看起来您正在执行While循环,然后执行for循环,然后执行if并返回for循环,直到完成所有等级。 you need to put both the student name and grade in the same for loop. 您需要将学生姓名和年级放在相同的for循环中。

 for(i=0; i<3; i++){
    System.out.println("What is the Students name?");
    Name = kb.next();//NAME
    System.out.println("What is the Students test score?");
    Grade = kb.nextDouble();//GRADE
    System.out.println();//SKIP SPACE ON SCREEN
    student[i]= new TestScores(Name, Grade); //PASSES ARGUMENT THEN STORES NAME AND GRADE WITH ARRAY ADDRESS  
   } 

    kb.close(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM