簡體   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