gpt4 book ai didi

java - 每次我编译项目时,while 循环之外的所有内容都不会显示(输入来自文件)

转载 作者:太空宇宙 更新时间:2023-11-04 14:00:03 25 4
gpt4 key购买 nike

import java.io.*;
import java.util.*;
public class Statistics {

public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new FileReader("C:\\Users\\user\\Desktop\\students.dat"));//Opened file

int math=0,spanish=0,french=0,english=0,count=0,sum,highSpan=0,highMath=0,highFren=0,highEng=0;//Declarations
double avg,highAvg=0;
String name,highName;
highName="";
name = ""; //Prime read
while (name!="ENDDATA"){ //Sentinel condition
count++; //Keeps count of students
name = in.next();
spanish=in.nextInt();
math=in.nextInt();
french=in.nextInt();
english=in.nextInt();
sum = spanish + math + french + english;
avg = sum/4; //Claculates Average
System.out.printf("%s - %.0f\n",name,avg);

if (avg > highAvg){ //Checks for the highest Average
highAvg = avg;
highName = name;
}
if (spanish > highSpan){
highSpan = spanish;
}
if (math > highMath){
highMath = math; //Checks for the highest mark for each subject
}
if (french > highFren){
highFren = french;
}
if (english > highEng){
highEng = english;
}

name = in.nextLine();
}

System.out.printf("Number of students in class is: %d\n",count);
System.out.printf("The highest student average is: %f\n",highAvg);
System.out.printf("The student who made the highest average is: %s\n",highName);
System.out.printf("The highest Spanish mark is: %d\n",highSpan);
System.out.printf("The highest Math mark is: %d\n",highMath);
System.out.printf("The highest French mark is: %d\n",highFren);
System.out.printf("The highest English mark is: %d\n",highEng);
in.close(); //Closing of file
}
}

编写一个Java程序来读取和处理文本文件的每一行并打印以下内容信息(除了最后一行之外的每一行):类(class)学生人数每个学生的姓名和平均分类(class)中最高的学生平均分(忽略平局的可能性)平均分最高的学生姓名(忽略平局的可能性)每个科目的最高分

它应该从 dat 文件中读取数据并进行必要的输出,但是 while 循环之外的打印语句没有显示,我也得到了带有以下错误的输出。

MARY - 65
SHELLY - 70
JOHN - 60
ALFRED - 71
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Statistics.main(Statistics.java:17)

最佳答案

in.nextInt() 抛出 NoSuchElementException。

您的文件似乎包含不正确的数据。

你的 while 循环永远不会中断,因为你的条件:

   while(name!="ENDDATA")

永远都是真的。这就是为什么当所有 token 都用完时,扫描仪对象会抛出 NoSuchElementException。并且由于出现异常,因此 while 循环之后的 print 语句不会被执行。

尝试使用扫描器的hasNext()方法来检查下一个 token 。

用这个 while 替换你的 while 循环。

  while (in.hasNext()){        //Sentinel condition
count++; //Keeps count of students
name = in.next();
if(name.equals("ENDDATA")){
break;
}
spanish=in.nextInt();
math=in.nextInt();
french=in.nextInt();
english=in.nextInt();
sum = spanish + math + french + english;
avg = sum/4; //Claculates Average
System.out.printf("%s - %.0f\n",name,avg);

if (avg > highAvg){ //Checks for the highest Average
highAvg = avg;
highName = name;
}
if (spanish > highSpan){
highSpan = spanish;
}
if (math > highMath){
highMath = math; //Checks for the highest mark for each subject
}
if (french > highFren){
highFren = french;
}
if (english > highEng){
highEng = english;
}


}

Students.dat 文件格式应如下所示:

Shelly 2 2 3 4
Brown 34 2 1 4
Pink 23 45 21 1
ENDDATA

关于java - 每次我编译项目时,while 循环之外的所有内容都不会显示(输入来自文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29382283/

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