Scanner用法

来源:互联网 发布:php str contains 编辑:程序博客网 时间:2024/04/30 04:35
  • 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.
package test1;

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);
        }
    }

}