繁体   English   中英

Java循环混乱

[英]Java Loop Confusion

我编写了这段旨在读取具有整数值的文件的代码。 如果整数值> = 0和<= 100,我需要给出等级的平均值。 如果有任何值超出指定范围0-100,那么我需要计算不正确的整数等级,将不正确的等级告知用户,并告知有多少不正确的等级。 我尝试了该代码,但始终收到错误代码:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Project9.main(Project9.java:26)

代码示例:

public static void main(String[] args) throws IOException{
    String file;
    int readInts;

    Scanner k = new Scanner(System.in);

    System.out.println("Enter filename: ");
    file = k.nextLine();
    int counterWrong = 0;
    int counterRight = 0;
    int sum = 0;
    double average = 1.0 * sum/counterRight;

    File fileReader = new File(file);

    if (fileReader.exists()) {
        Scanner input = new Scanner(fileReader);
        while (input.hasNext()) {
            readInts = input.nextInt();
            System.out.println(readInts);
            String a = input.next();
            int a2 = Integer.parseInt(a);

        if (a2 <= 100 && a2 >= 0){
            counterRight++;            
            sum = sum + a2; 
            System.out.println("Score " + a2 + " was counted.");

        } else {
            counterWrong++;
            System.out.println("The test grade " + a2 + " was not scored as it was out of the range of valid scores.");
            System.out.println("There were " + counterWrong + " invalid scores that were not counted.");
        }
        }
        if (counterRight > 0){
            System.out.println("The average of the correct grades on file is " + average + ".");
        }
    } else {
        System.out.println("The file " + file + " does not exist. The program will now close.");
    }


}

}

您正在执行一次hasNext检查,但是随后使用nextInt()next()从扫描仪读取了两次。

使用hasNextInt()代替hasNext()。

hasNext()仅表示存在另一个令牌,并不一定意味着您在编写nextInt()时假设存在另一个整数。

这是hasNext()hasNextInt()的文档

您还想在此行之前进行检查:

String a = input.next();

我看到的代码可能存在两个问题。

  1. 文件= k.nextLine(); //根据文件的设置方式,k.nextLine()或k.next()或k.nextInt()可能有用。

  2. while(input.hasNext()){readInts = input.nextInt(); // input.hasNext()假定扫描仪正在读取的下一个值具有字符串值,该字符串值将使readInts = input.nextInt(); 未经解析(或其他方法)无法使用。

我认为尝试此练习会很有趣(不想为您毁了它)。 查看我的代码,希望您能学到我正在谈论的一些概念。

注意:我的程序读取整数值,例如95 185 23 13 90 93 37 125 172 99 54 148 53 36 181 127 85 122 195 45 79 14 19 88 34 73 92 97 200 167 126 48 10938。使用hasNext()和next ()获取列出的所有令牌。 因此,对于给定的输入,使用nextLine()不会有用。

封装cs1410;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JFileChooser;

public class Grader {

    public static void main(String[] args) throws IOException {
        int count = 0;
        int sum = 0;
        double ave = 0;
        int incorrectCount = 0;
        String correctGrades = "";
        String incorrectGrades = "";

        // Read file input
        JFileChooser chooser = new JFileChooser();
        if (JFileChooser.APPROVE_OPTION != chooser.showOpenDialog(null)) {
            return;
        }
        File file = chooser.getSelectedFile();

        // Scan chosen document
        Scanner s = new Scanner(file);


        // While the document has an Int
        while (s.hasNextInt()) {
            // Convert our inputs into an int
            int grade = Integer.parseInt(s.next());

            if (grade >= 0 && grade <= 100) {
                // adds sum
                sum += grade;
                // increments correct count
                count++;
                // displays valid grades
                correctGrades += Integer.toString(grade) + "\n";
            } else {
                // increments incorrect count
                incorrectCount++;
                // displays invalid grades
                incorrectGrades += Integer.toString(grade) + "\n";
            }
        }
        // Created average variable 
        ave = sum / count;

        // bada bing bada boom
        System.out.println("The number of correct grades were " + correctGrades);
        System.out.println("The average score on this test was " + ave + "\n");
        System.out.println("The number of incorrect grades were " + incorrectCount + "\n");
        System.out.println("The incorrect values for the grades were " + "\n" + incorrectGrades);

    }

}

暂无
暂无

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

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