- Scanner is suitable to read input data for the most of problems, but it is very slow. You should use it to read small input data only.
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int a,b;
while(cin.hasNextInt()){
a = cin.nextInt();
b = cin.nextInt();
System.out.println(a+b);
}
}
}
下面这种用法也是正确的:
package test1;
import java.util.Scanner;
class YouCanUseSuchClasses {}
public class Main2 {
class AndSuchClassesToo {}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int a, b;
while(cin.hasNextInt())
{
a = cin.nextInt();
b = cin.nextInt();
System.out.println(a + b);
}
}
}