A+B for Input-Output Practice (I) 
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)  
Total Submission(s): 9369 Accepted Submission(s): 4523   
Problem DescriptionYour task is to Calculate a + b.
Too easy?! Of course! I specially designed the problem for acm beginners. 
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.  
 
InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.  
 
OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.  
 
Sample Input
1 5
10 20 
 
Sample Output
6
30 
 

解决方案 »

  1.   


    //输入范例:
    //3 6
    //5 8
    //end
    List<Integer> result = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    String s = null;
    boolean flag = true;
    System.out.println("请输入若干对整数,输入“end”表示结束:");
    do{
    s = sc.nextLine();
    if(s.equals("end")) {
    System.out.println("\n求和结果为:");
    for(Integer x:result) {
    System.out.println(x);
    }
    flag = false;
    }else {
    Scanner scanner = new Scanner(s);
    int a = scanner.nextInt();
    int b = scanner.nextInt();
    result.add(a+b);
    }
    }while(flag);
      

  2.   

    楼上的大侠,不能有“end”。
      

  3.   

    import java.util.Scanner;class Main {
    public static void main(String[] args) { int a, b;
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNextInt()) {
    a = scanner.nextInt();
    b = scanner.nextInt();
    System.out.println(a + b);
    }
    }
    }
      

  4.   

    import java.util.Scanner;class Main {
    public static void main(String[] args) { int a, b;
    Scanner scanner = new Scanner(System.in); a = scanner.nextInt();
    b = scanner.nextInt();
    System.out.println(a + b); }
    }