nextInt():用于只读取整型数据,读入空白或空白字符,会跳过,以换行符’\n’结束输入,不包含换行符。
1个数字一个数字的读
nextLine():读取整行,包括换行符
一行一行的读
next():读取单个单词,以空格作为分隔符
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 思路: 先读取N和M,
// for循环M次,一次一行,一行读三个nextInt
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int N = sc.nextInt();
int M = sc.nextInt();
System.out.println(String.format("%d %d",N, M));
for (int i = 0; i < M; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println(String.format("%d %d %d", a, b, c));
}
}
}
}