好的,所以我应该从文本文件中读取和打印,但每次运行时我都会在第31行“grade = in.nextInt();”中得到“java.utilNoSuchElementException”。当前的文本文件是 2 80 97 5 69 79 89 99 58 7 60 70 80 90 100 0 59
其中第一个数字是每个部分的分数,每个部分应该被计算(即1,2,3)一次一个问题。这是当前的代码。
import java.util.Scanner; import java.io。*;
公共类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: ");
}
}
答案 0 :(得分:0)
试试这个
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;}
}
}