对于我的任务,我要制作一个程序,从文本文件中读取和打印数字,然后计算这些数字的总和和平均值.我的程序做得很好.我唯一的问题是程序不会读取我的文本文件中的最后一个数字.文件上的数字为:
3
8
1
13
18
15
7
17
1
14
0
12
3
2
5
4
Run Code Online (Sandbox Code Playgroud)
由于某种原因,计算机将无法读取数字4.
这是我的计划:
{ //begin testshell
public static void main (String[] args)
{ //begin main
System.out.println("Scores");
Scanner inFile=null;
try
{
inFile = new Scanner(new File("ints.dat"));
}
catch (FileNotFoundException e)
{
System.out.println ("File not found!");
// Stop program if no file found
System.exit (0);
}
// sets sum at 0 so numbers will be added
int sum=0;
int num= inFile.nextInt();
// starts counting the amount …Run Code Online (Sandbox Code Playgroud) java ×1