gpt4 book ai didi

java - 从文本文件读取并打印

转载 作者:行者123 更新时间:2023-12-01 04:17:11 24 4
gpt4 key购买 nike

好吧,所以我应该使用代码从文本文件中读取和打印,但每次运行时,我都会在第 31 行“grade = in.nextInt();”上收到“java.utilNoSuchElementException”。当前文本文件是2 80 975 69 79 89 99 587 60 70 80 90 100 0 59

其中第一个数字是每个部分的分数数量,每个部分都应该被计算在内(即1、2、3)无论如何,一次一个问题。这是当前的代码。

导入java.util.Scanner;导入java.io.*;

公共(public)课Prog2{

public static void main (String args []) throws IOException
{
Scanner in = new Scanner (new File ("test1.txt"));

int Lowest, Highest, grade = 0;
float section_average, class_average;
int count = 0, A = 0, B = 0, C = 0, D = 0, F = 0, total_number_of_sections = 0, total_number_of_scores = 0, number = 0;
while (in.hasNextInt())
{
number = in.nextInt();
System.out.println (in.nextInt());
number ++;

while (count < number)
{
grade = in.nextInt();
total_number_of_sections += number;
total_number_of_scores += grade;
total_number_of_scores ++;

count++;


}

}
if (number > 0)
{
System.out.println ("Scores for section "+count);
}
else
{
System.out.println ("Scores for section 0");
}
if (grade >= 90)
{
A ++;

}
if (grade >= 80 && grade < 90)
{
B ++;

}
if (grade >= 70 && grade < 80)
{
C ++;

}
if (grade >= 60 && grade < 70)
{
D ++;

}
if (grade < 60)
{
F ++;

}
System.out.println (" ");
System.out.println ("Scores for section "+count);
System.out.println ("A's" + A);
System.out.println ("B's" + B);
System.out.println ("C's" + C);
System.out.println ("D's" + D);
System.out.println ("F's" + F);
System.out.println ("Lowest Score: ");
System.out.println ("Highest Score: ");
System.out.println (" ");
System.out.println (" ");
System.out.println ("Total number of sections: " + total_number_of_sections);
System.out.println ("Total number of scores: " + total_number_of_scores);
System.out.println ("Class Average: ");



}

}

最佳答案

试试这个

while (in.hasNextInt())
{
count = 0;
number = in.nextInt();
System.out.println (in.nextInt());


while (in.hasNextInt())
{
grade = in.nextInt();
total_number_of_sections += number;
total_number_of_scores += grade;
total_number_of_scores ++;

if(++count == number ){ break;}


}

}

关于java - 从文本文件读取并打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19331212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com