说明
ACM赛制的主类必须是class Main 或者是public class Main,方法入口必须是主方法。
Java中的输入和输出
如果要混合使用nextInt读入一个整数和读入一行nextLine,需要先搞读入一行nextLine顺序才能正确。
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int n = sc.nextInt();
String strs[] = new String[n];
sc.nextLine();
for (int i = 0; i < n; i++) {
strs[i] = sc.nextLine();
}
System.out.println(Arrays.toString(strs));
scanner.hasNext()可以让输入无限循环,当使用nextInt的时候,如果是使用空格作为分隔符,空格前面的被接收到第一个nextInt,空格后面的被接收到第一个nextInt。
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
System.out.println(scanner.nextInt());
System.out.println(scanner.nextInt());
while (scanner.hasNext()) {
System.out.println(scanner.nextInt());
}
当输入0 0时结束
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
while (scanner.hasNext()) {
int a = scanner.nextInt();
int b = scanner.nextInt();
if (a == 0 && b == 0) {
break;
}else{
System.out.println(a + b);
}
对于数组赋值
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
int n = scanner.nextInt();
String[] arr = new String[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.next();
}
System.out.println(Arrays.toString(arr));