Java中如何从键盘输入内容: import java.util.Scanner; .nextLine(); .hasNextInt();

 

 

import java.util.Scanner;
public class Reader
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in); //创建对象

        System.out.println("input your string: ");
        String str = scan.nextLine();
        System.out.println(str);

        System.out.println("input your number: ");
        int num = scan.nextInt();
        System.out.println(num);

        System.out.println("input your number: ");
        if (scan.hasNextInt()) //判断输入的是否是整数
        {
            System.out.println(scan.nextInt());
        }
        else
            System.out.println("not an integer.");
    }
}

 

上一篇:Scanner初学


下一篇:Scanner类及其中方法的使用;