您的第一个值(“ Michael”) 不是 整数,因此它永远不会进入循环主体。
也许您想更改代码以使其循环播放,直到到达文件末尾,读取并打印整数,但使用(不打印)非整数值。所以像这样:
import java.util.*;import java.io.*;public class Test { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new File("test.txt")); while (sc.hasNext()) { if (sc.hasNextInt()) { System.out.println(sc.nextInt()); } else { // Just consume the next token sc.next(); } } sc.close(); }}


