2011-05-14 回答
public class Test {
public static int a=0;
public static void main(String[] args)
{
while(true)
{
try
{
Scanner input = new Scanner(System.in);
a=input.nextInt();
if(-1==a){
throw new IOException("请输入大于-1的数");
}else{
return;
}
}
//这个地方怎么实现IO异常
catch(IOException e)
{
System.out.println("erro is a,请从新输入:");
}
}
//在while后面的语句怎么都会报错
追问:
异常都不是关键,关键是这里会报错
//在while后面的语句怎么都会报错?
System.out.print(a);
Test.prints();
a=a+0;
追答:
你要说报什么错我才知道
追问:
no suggestionsav ailable
追答:
不可到达语句
import java.io.IOException;
import java.util.Scanner;
public class Test {
public static int a = 0;
public static void main(String[] args) {
boolean b = true;
while (b) {
try {
Scanner input = new Scanner(System.in);
a = input.nextInt();
if (-1 == a) {
throw new IOException("请输入大于-1的数");
} else {
b = false;
}
} //这个地方怎么实现IO异常
catch (IOException e) {
System.out.println("erro is a,请从新输入:");
}
}
//在while后面的语句怎么都会报错?
System.out.print(a);
Test.prints();
a = a + 0;
}
public static void prints() {
System.out.println(a);
}
}
改成这样的
追问:
怎么还是不能实现输入错误,再次输入。。。我加分!你搞定
追答:
import java.io.IOException;
import java.util.Scanner;
public class Test {
public static int a = 0;
public static void main(String[] args) {
boolean b = true;
while (b) {
try {
Scanner input = new Scanner(System.in);
a = input.nextInt();
if (5 > a) {
throw new IOException(a + "<5");
} else {
b = false;
}
} catch (IOException e) { //这个地方怎么实现IO异常
System.out.println(e.getMessage());
}
}
System.out.println(a);
Test.prints();
a = a + 0;
}
public static void prints() {
System.out.println(a);
}
}
你输入比5小的就可以报错了