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)?
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)?
解决方案 »
- SWING 透明按钮问题
- 关于JMenubar,请高手帮下忙!
- String findInLine( Pattern pattern) 如何使用
- 怎样用DataInputStream从文件读入String?
- SQL Server2000的存储过程如果有2个以上不同的select语句在JAVA中如何得到这几个结果集?
- Proxy Error,我用tomcat + apache出错了,里面有重写规则
- Java中Object数组对象转型泛型对象数组
- 哪有jb8_encoding_fix.jar,哪位大虾指点
- 急问:applet与servlet通讯,请教高手!
- nitifyAll和return语句的问题
- JAVA问题
- 关于正则表达式的意思
System.out.print("Please Input:");
str=in.readLine();
guessNum=Integer.parseInt(str);
}
catch(Exception e){
}
这里是读取输入的整数,不能省;
catch(Exception e)可以改成catch(IOException e)
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语句?
因为NumberFormatException没继承IOException,所以还是得用Exception
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!");
}
}
上面的代码应该可以运行把???
第十四行错了:System.ut.print("Please Input:");//!!!!