作者:651404449_724afc | 来源:互联网 | 2022-12-02 17:11
为什么我的程序在从文件中读取时会忽略零?例如,以下是文件中的数字:
0001 0011 0010
那么这是我的输出:
1
11
10
这是我的代码:
File file = new File("num.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
System.out.println(scanner.nextInt());
} else {
scanner.next();
}
}
Grant Foster..
6
用scanner.next()
而不是scanner.nextInt()
.
使用scanner.nextInt()
将删除任何前导零,因为0001 == 1.
1> Grant Foster..:
用scanner.next()
而不是scanner.nextInt()
.
使用scanner.nextInt()
将删除任何前导零,因为0001 == 1.