import java.util.Scanner;public class LuckGuess { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        int rn = (int)(Math.random()*100);
        System.out.println("请猜一个0到99的数:");
        while(true)
        {
         int gn = 0;

gn = scan.nextInt();

// TODO Auto-generated catch block
System.out.println("你输入的是:"+scan.next());
System.out.println("只能猜数字,请重新输入:");
continue;

         if(rn == gn)
         {
         System.out.println("太强了,猜对了。");
         break;
         }else if(rn > gn){
         System.out.println("你猜的数小了。");
         }else{
         System.out.println("你猜的数大了。");
         }
        
        }
        
}}

解决方案 »

  1.   

    但是如果加点东西这个程序就正确了
    import java.util.Scanner;public class LuckGuess { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
            Scanner scan = new Scanner(System.in);
            int rn = (int)(Math.random()*100);
            System.out.println("请猜一个0到99的数:");
            while(true)
            {
             int gn = 0;
    try {
    gn = scan.nextInt();
    } catch (RuntimeException e) {
    // TODO Auto-generated catch block
    System.out.println("你输入的是:"+scan.next());
    System.out.println("只能猜数字,请重新输入:");
    continue;
    }
             if(rn == gn)
             {
             System.out.println("太强了,猜对了。");
             break;
             }else if(rn > gn){
             System.out.println("你猜的数小了。");
             }else{
             System.out.println("你猜的数大了。");
             }
            
            }
            
    }}不知道为什么加个try {catch (RuntimeException e) {就可以了撒
      

  2.   

    Scanner.nextInt()会抛出一下三种异常,必须用try{}catch{}捕捉
    InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围 
    NoSuchElementException - 如果输入信息已耗尽 
    IllegalStateException - 如果此扫描器已关闭RuntimeException是这三个异常类的父类,所以用RuntimeException就全部捕捉到了