繁体   English   中英

为什么在 JAVA 中使用扫描仪时出现运行时错误

[英]Why I am getting a RUNTIME ERROR when using Scanner in JAVA

问题:

找出最后一次信息学考试的 D、C、B 和 A 成绩的数量,其中来自 class 的 n 个学生成功通过了考试。

在这个任务中,我们使用 5 分评分系统,只对及格分数感兴趣:从 2 到 5。它们对应的字母等级如下:5 代表 A,4 代表 B,3 代表 C 2 代表 D。程序获取数字 n 作为输入,然后自己获取成绩:一个接一个。

程序应 output 四个数字在一行中:分别为 D、C、B 和 A 级数。

import java.util.Scanner;

class Main {
public static void main(String[] args) {
    // put your code here
    Scanner scan = new Scanner(System.in);

    int numStudents = scan.nextInt();
    int marks;

    int gradeA = 0;
    int gradeB = 0;
    int gradeC = 0;
    int gradeD = 0;

    for (int i = 0; i <= numStudents; i++){
        marks = scan.nextInt();
        if(marks == 5){
            gradeA++;
        } else if (marks == 4){
            gradeB++;
        } else if (marks == 3){
            gradeC++;
        } else if (marks == 1){
            gradeD++;
        }
    }

    System.out.println(gradeD);
    System.out.println(gradeC);
    System.out.println(gradeB);
    System.out.println(gradeA);
}

}

错误:

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Main.main(Main.java:17)

如果您将numStudents定义为 5,那么您应该输入 6(因为<=在您的 for 循环中)

for (int i = 0; i <= numStudents; i++){

这应该是..

for (int i = 0; i < numStudents; i++){

当您在标准输入中输入1作为第一个输入时,它将是 numStudents 现在,由于<= ,您必须输入 2 个输入。 所以你的标准输入应该是

1
2
3

相反,最好在您的代码中将<=更改为< ,以便您可以输入1 2

当您使用代码在标准输入中仅给出1 2时(使用<= ),您会得到NoSuchElementException

如果扫描仪上没有 integer,您将收到此错误。 你必须提出一个条件。 尝试:

if(scan.hasNextInt()) { 
marks = scan.nextInt(); 
}

对于D级,if 条件应为else if (marks == 2) 你放了1而不是2

我在编辑器中运行了您的代码,修复了循环中的一些小问题,并且学生人数检查它在输入中运行良好:

5
2 3 4 5 6

这是整个代码块:

  import java.util.Scanner;

  public class HelloWorld{

     public static void main(String []args){
        
        Scanner scan = new Scanner(System.in);
    
        if(scan.hasNextInt() ){
           
        
            int numStudents = scan.nextInt();
            int marks;
        
            int gradeA = 0;
            int gradeB = 0;
            int gradeC = 0;
            int gradeD = 0;
        
            for (int i = 0; i < numStudents; i++){
                marks = scan.nextInt();
                if(marks == 5){
                    gradeA++;
                } else if (marks == 4){
                    gradeB++;
                } else if (marks == 3){
                    gradeC++;
                } else if (marks == 1){
                    gradeD++;
                }
            }
        
            System.out.println(gradeD);
            System.out.println(gradeC);
            System.out.println(gradeB);
            System.out.println(gradeA);
        
            }
       }
   }

在这种情况下,这是 output:

0
1
1
1

暂无
暂无

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

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