【发布时间】:2014-03-17 07:11:21
【问题描述】:
在学校,我们正在学习编写一个程序,该程序将从名为“Names.txt”的文本文档中读取一个人的全名。我们必须让文件夹中的名字输出名字的第一个字母,然后是完整的姓氏。所有这些都必须在我们对 while 循环、for 循环、if else 语句和布尔值的基本知识之下。我的代码有问题:
Scanner scFile = new Scanner (new File ("Names.txt"));
String line = "", name = "";
int num, sum = 0;
while (scFile.hasNext())
{
Scanner scLine = new Scanner (line).useDelimiter("#");
name = scLine.next();
num = scLine.nextInt();
scLine.close();
System.out.println(name.substring(0,1) + "\t" + num);
sum = sum+ num;
}
scFile.close();
System.out.println("The sum is " + sum);
【问题讨论】: