我正在尝试从格式如下的文本文件中读取输入:
2 80 97
5 69 79 89 99 58
7 60 70 80 90 100 0 59
每行的第一个数字是每个“部分”的“等级”数。
我让我的程序读取一个部分,但我不知道如何让它读取将有多少部分,然后读取下一行。
我想我可以把我当前的代码放在一个计数控制的循环中,首先读取有多少部分,然后多次运行循环。我只是不知道如何将这个想法转换为代码。
这是revevant代码部分:
public static void main(String args[]) throws Exception
{
Scanner in = new Scanner(new File("prog2test.txt"));
//int sections = (in.nextInt());
int scores = (in.nextInt());
int scoresForAverage = scores;
int scoreTotals = 0;
double average = 0;
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int F = 0;
int highest = 0;
int lowest = 100;
while (scores > 0 && in.hasNextInt())
{
int grade = in.nextInt();
if (grade >= 90)
A++;
else if (grade >= 80)
B++;
else if (grade >= 70)
C++;
else if (grade >= 60)
D++;
else
F++;
if (grade > highest)
highest = grade;
if (grade < lowest)
lowest = grade;
scores--;
scoreTotals = (scoreTotals + grade);
}
average = scoreTotals/scoresForAverage;
System.out.println("Scores for section 1");
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: " + lowest);
System.out.println("Highest score: " + highest);
System.out.println("Average: " + average);
编辑:用完整的方法更新。