关于Scanner.nextInt和Scanner.hasNextInt
首先,Scanner.nextInt()和Scanner.hasNextInt()都可以输入
Scanner.hasNextInt()返回的是bool型
exp:
import java.util.Scanner;
public class hellowwold {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);scanner.hasNextInt();int num0 = scanner.nextInt();System.out.println(num0);System.out.println(scanner.hasNextInt());int num1 = scanner.nextInt();System.out.println(num1);}
}
输入 6 5
//输出
6
6
5
true
5
当hasNextInt()接收到一个值后会保存下来 传给下一个 nextInt()
所以这里的num0,num1 不需重新再输入