刚入门,有大神能回答吗  百度没找到。

解决方案 »

  1.   

    Scanner sc = new Scanner;
    sc.nextInt()接收呀
      

  2.   


    import java.util.Scanner;public class Test{
    public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    int time = 0;
    String content = null;
    while(time ++ < 4){
    System.out.print("第" + time + "次输入一个整数:");
    content = keyboard.nextLine();
    if(!content.matches("^\\s*\\d+\\s*$")){
    System.out.println("输入的内容'" + content + "'无效");
    System.exit(1);
    }else{
    System.out.println("输入的内容'" + content + "'有效");
    }
    }
    }
    }
      

  3.   

     public static void main(String[] args) {
            // TODO Auto-generated method stub
         Scanner input = new Scanner(System.in);
            String str = input.nextLine();
            String [] contents = str.split(input.delimiter().toString());
            System.out.println("输入的内容'" + str + "'!");
            System.out.println("输入的数量:'" +contents.length + "'!");
            if(contents.length != 4){
             System.out.println("请输入4个数字!");
            }
            else{
                for(int i = 0; i < contents.length; i++){
                    if(!contents[i].matches("^\\s*\\d+\\s*$")){
                     System.out.println("请输入4个数字!");
                        System.exit(1);
                    }else{
                        System.out.println("输入第"+ (i + 1)+"的数字:" + contents[i] + "!");
                    }
                }
            }
            
        }
      

  4.   

    定义一个int类型的数组,长度为4,再用for循环里面写个接受语句,然后用try catch当长度不为4时给出提示语句
      

  5.   

    直接用nextInt()逐个读取然后保存起来,这些读取的语句用try   catch语句包起来,如果出现异常就返回并提示报错,停止数据输入并停止程序。
      

  6.   

    import java.util.Scanner;public class test7 { public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    int count = 1;
    int[] s = new int[4];
    while (count < 5) {
    System.out.println("请输入第" + count + "个整数");
    try {
    s[count - 1] = sc.nextInt();
    count++;
    } catch (Exception e) {
    // TODO Auto-generated catch block
    System.out.println("输入不正确请重新输入");
    sc.next();
    }
    }
    for (int i = 0; i < 4; i++) {
    System.out.print(s[i] + "\t");
    }
    }
    }