mport java.io.*;
public class basic
{public static void main(String args[])
 {
   InputStreamReader ir;
   BufferedReader in;
   ir =new InputStreamReader(System.in);
   in =new BufferedReader(ir);
   int ranNum=(int)(Math.random()*10+1);
   int guessNum=-1;
   String str;
   while(guessNum!=ranNum)
   {
    try{
        System.out.print("Please Input:");
        str=in.readLine();  
        guessNum=Integer.parseInt(str);
       }
  catch(Exception e){
       }
        if(guessNum>ranNum)
        System.out.print("Try smaller......");
     else
        System.out.print("Try bigger......");
  }
   System.out.print("Great!");
 }
}
上面红色代码处能不能省掉不要?而且catch(Exception e)能不能改成catch(IOException e)?

解决方案 »

  1.   

    try{ 
            System.out.print("Please Input:"); 
            str=in.readLine();   
            guessNum=Integer.parseInt(str); 
           } 
      catch(Exception e){ 
           }   
    这里是读取输入的整数,不能省;    
      

  2.   

    红色代码必须保留,不能去掉,
    catch(Exception e)可以改成catch(IOException e)
      

  3.   

    IOException类是Exception的子类,如果能确定抛出的是IOException,那么可以替换.
      

  4.   

    iport java.io.*; 
    public class basic 
    {public static void main(String args[]) 
     { 
       InputStreamReader ir; 
       BufferedReader in; 
       ir =new InputStreamReader(System.in); 
       in =new BufferedReader(ir); 
       int ranNum=(int)(Math.random()*10+1); 
       int guessNum=-1; 
       String str; 
       while(guessNum!=ranNum) 
       { 
        try{ 
            System.out.print("Please Input:"); 
            str=in.readLine();   
            guessNum=Integer.parseInt(str); 
           } 
      catch(Exception e){ 
           }        if(guessNum>ranNum) 
            System.out.print("Try smaller......"); 
         else 
            System.out.print("Try bigger......"); 
      } 
       System.out.print("Great!"); 
     } 

    上面代码能否改成以下内容:
    iport java.io.*; 
    public class basic 
    {public static void main(String args[]) 
     { 
       InputStreamReader ir; 
       BufferedReader in; 
       ir =new InputStreamReader(System.in); 
       in =new BufferedReader(ir); 
       int ranNum=(int)(Math.random()*10+1); 
       int guessNum=-1; 
       String str; 
       while(guessNum!=ranNum) 
       { 
        System.ut.print("Please Input:"); 
            str=in.readLine();   
            guessNum=Integer.parseInt(str); 
           if(guessNum>ranNum) 
            System.out.print("Try smaller......"); 
         else 
            System.out.print("Try bigger......"); 
      } 
       System.out.print("Great!"); 
     } 

    不要try...catch语句?
      

  5.   

    不能改为IOException,原因如下:因为你用Integer.parseInt(str) 的时候,可能出现这个异常NumberFormatException,比如Integer.parseInt("acv");
    因为NumberFormatException没继承IOException,所以还是得用Exception
      

  6.   

    去掉try语句后可以改为以下语句吗:
    import java.io.*;  
    public class basic  
    {public static void main(String args[])  throws Exception  
     {  
       InputStreamReader ir;  
       BufferedReader in;  
       ir =new InputStreamReader(System.in);  
       in =new BufferedReader(ir);  
       int ranNum=(int)(Math.random()*10+1);  
       int guessNum=-1;  
       String str;  
       while(guessNum!=ranNum)  
       {  
        System.ut.print("Please Input:");  
            str=in.readLine();    
            guessNum=Integer.parseInt(str);  
           if(guessNum>ranNum)  
            System.out.print("Try smaller......");  
         else  
            System.out.print("Try bigger......");  
      }  
       System.out.print("Great!");  
     }  
    }  
    上面的代码应该可以运行把???
      

  7.   

    你运行试下...
    第十四行错了:System.ut.print("Please Input:");//!!!!