/* 从键盘接收输入。输入数字就退出,不是数字就继续输入*/
import java.io.*;
public class StandInt1 {
public static int c=0;
public static void main(String[] args) throws IOException{
         int a[]={48,49,50,51,52,53,54,55,56,57};
test1:
while (true) {
System.out.println("请输入一个数字:");
c=(int)System.in.read();
test2:
for (int i = 0;i<10;i++) {
if (c==a[i]) {
System.out.println(c);
break test1;
}
else 
continue test2;
}
}
}
}
控制台输出
c:>java StandInt1
请输入一个数字:
a
请输入一个数字:
请输入一个数字:
请输入一个数字:
b
请输入一个数字:
请输入一个数字:
请输入一个数字:
1
49c:>为什么会有三个 “请输入一个数字:”出现的? 
我本来的意思是想 输入错了就要求只输出一个 “请输入一个数字:”,让用户继续输入。
求各位大虾帮帮忙

解决方案 »

  1.   

    你输入a,按下回车的时候  相当于输入  a\n  三个字符,自然就有3次了
      

  2.   

    帮你稍微修改了下,你看下···
    你在写的时候出现问题的原因是在回车符 public static int c=0;
        public static void main(String[] args){
         Scanner scan = new Scanner(System.in);
            int a[]={48,49,50,51,52,53,54,55,56,57};   
            String s = "";;
            
            test1:
            while (true) {
                System.out.println("请输入一个数字:");
                try {
    c = scan.nextInt();//用于接收一个整数,若是字符进入错误处理
    } catch (Exception e) {
    // TODO Auto-generated catch block
    System.out.println("输入错误,请重新输入");
    s = scan.next();//这个地方是为了去掉你当前输入的回车符,没有这句话会出现死循环
    continue test1;
    }
                

                test2:
                for (int i = 0;i<10;i++) {
                    if (c==a[i]) {
                        System.out.println(c);
                        break test1;
                    }
                    else 
                        continue test2;
                }
            }
        }
      

  3.   


    public static String c = "";
    public static int d = 0;
        public static boolean flag=false;
    public static void main(String[] args) throws IOException {
    int a[] = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 };
    while (true) {
    System.out.println("请输入一个数字:");
    Scanner sc = new Scanner(System.in);
    c = sc.next();
    if (c.matches("\\d+")) {
    try {
    d = Integer.parseInt(c);
    } catch (Exception e) {
    continue;
    }
    } else
    continue;
    for (int i = 0; i < 10; i++) {
    if (d == a[i]) {
    System.out.println(d);
    flag=true;
    break;
    }
    }
    if(flag){
    sc.close();
    break;
    }

    }
    }
      

  4.   

    package com.pojo;
    import java.io.IOException;
    public class sa {
        public static int n=0;
        public static byte[] keyborad=new byte[100]; 
        //用ascii码   
        public static boolean isNumeric(String str){   
           for(int i=0;i<str.length()-2;i++){   
              int chr=str.charAt(i);   
              if(chr<48 || chr>57)   
                 return false;   
           }   
           return true;   
        } 
        public static void main(String[] args) throws IOException{
                 int a[]={48,49,50,51,52,53,54,55,56,57};    
            test1:
            while (true) {
                System.out.println("请输入一个数字:");
               n=System.in.read(keyborad);
               String c=new String(keyborad,0,n);
                    if (isNumeric(c)) {
                        System.out.println(c);
                        break test1;
                    }
                    else{
                        continue test1;
                    }
                }
            }
    }