不知道什么原因??(100分)

  • 主题发起人 主题发起人 夜游神宾
  • 开始时间 开始时间

夜游神宾

Unregistered / Unconfirmed
GUEST, unregistred user!
public class a
{
public static void main(String[] args)
{
char a;
System.out.println("Please Into a Number 1-3");
a = (char)System.in.read();
switch(a)
{
case '1':
System.out.println("your into Number is 1");
break;
case '2':
System.out.println("your into Number is 2");
break;
case '3':
System.out.println("your into Number is 2");
break;
default:
System.out.println("your into Number is "+a);
break;
}
}
}
javac a.java
错误提示如下:

a.java:8: unreported exception java.io.IOException;
must be caught or declared to be thrown
a = (char)System.in.read();
^
1 error
这是为什么,我没看出错误???
请各位大侠多多指教我(刚刚开始学java两天)。
 
好象是因为System.in.read()方法中定义了可能的抛出异常,你必需对此可能发生的
异常进行处理。用Try..except语句。
不要问我更多的东西,我没有学过Java。
 
hanyongjian是对的。
system.in.read方法申明抛出io异常,你应该主动捕获这个异常。
修改后的代码如下:
try{
a = (char)System.in.read();
switch(a)
{
case '1':
System.out.println("your into Number is 1");
break;
case '2':
System.out.println("your into Number is 2");
break;
case '3':
System.out.println("your into Number is 2");
break;
default:
System.out.println("your into Number is "+a);
break;
}
}catch(IOException ioexp){
System.out.println("there is a IOException occured,the detailinfo is ");
ioexp.printStack();
}
 
如此更改后,错误提示如下:
C:/>javac a.java
a.java:27: cannot resolve symbol
symbol : class IOException
location: class a
catch(IOException ioexp)
^
1 error
还是过不去,请指教,这到底是什么原因?
 
to fbyang :你有什么意见吗?说来听听。
 
要import java.io.*;吧
 
各位可以在本机上试一试。谁能告诉我??
 
import java.io.*;
 
import java.io.*;
public class a
{
public static void main(String[] args)
{
char a;
System.out.println("Please Into a Number 1-3");
a = (char)System.in.read();
switch(a)
{
case '1':
System.out.println("your into Number is 1");
break;
case '2':
System.out.println("your into Number is 2");
break;
case '3':
System.out.println("your into Number is 2");
break;
default:
System.out.println("your into Number is "+a);
break;
}
}
}
javac a.java
error hint:
C:/>javac a.java
a.java:9: unreported exception java.io.IOException;
must be caught or declared t
o be thrown
a = (char)System.in.read();
^
1 error
为什么????????????我要疯了!!!?????
 
在main() 后 加一句 throws IOException 就可以通过了
import java.io.*;
public class a
{
public static void main(String[] args) throws IOException
{
char a;
System.out.println("Please Into a Number 1-3");
a = (char)System.in.read();
switch(a)
{
case '1':
System.out.println("your into Number is 1");
break;
case '2':
System.out.println("your into Number is 2");
break;
case '3':
System.out.println("your into Number is 2");
break;
default:
System.out.println("your into Number is "+a);
break;
}
}
}
 
兄弟,看书啊,随便找本书都有解释的,我懒得帮你了。
 
我是初学者耶,输入输出流还没有看到呢,如果不是这样,我也不至于落魄到如此地步。
多谢各位侠中客来光临指导,以后还请多多帮助。谢谢!
 

Similar threads

A
回复
0
查看
932
Andreas Hausladen
A
A
回复
0
查看
805
Andreas Hausladen
A
I
回复
0
查看
3K
import
I
I
回复
0
查看
738
import
I
I
回复
0
查看
602
import
I
后退
顶部