我运行了一下,报这样的错: --------------------Configuration: j2sdk1.4.2 <Default>-------------------- C:\Documents and Settings\Administrator\桌面\Maxmin.java:8: unreported exception java.io.IOException; must be caught or declared to be thrown a[0]=System.in.read( ); ^ C:\Documents and Settings\Administrator\桌面\Maxmin.java:13: unreported exception java.io.IOException; must be caught or declared to be thrown a[i]=System.in.read( ); 我也搞不明白,期待高手的解答。 ^
import java.io.*; public class Test { public static void main(String agrs[]) { int max,min; int a[]=new int[10]; try{ a[0]=System.in.read( ); max=a[0]; min=a[0]; for(int i=1;i<10;i++) { a[i]=System.in.read( ); if(a[i]>max) max=a[i]; if(a[i]<min) min=a[i]; System.out.println(max); System.out.println(min); } }catch(IOException e){ } } }
max和min必须初始化,还有,上面有那么多仁兄用了 System.in.read(),这怎么会行呢?程序能运行也不能 得到正确解答! 正解如下: import java.io.*; public class Test { public static void main(String agrs[]) { int max=0,min=0; int a[]=new int[10]; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try{ a[0]=Integer.parseInt(br.readLine()); max=a[0]; min=a[0]; for(int i=1;i<10;i++) { a[i]=Integer.parseInt(br.readLine()); if(a[i]>max) max=a[i]; if(a[i]<min) min=a[i]; } }catch(IOException e){ } System.out.println(max); System.out.println(min); } }
/* 呵呵,其实用BufferedReader来包装一个System.in在JAVA中真的算是 最简单的了,大家不能将JAVA的输入输出流与C++中的输入输出流来看, 因为JAVA中关于输入输出流,大大小小的类,真的是远比C++多,而且 提供的流类大多是一些低层的类,哪怕要最简单的从键盘输入一个整数 这样的直接的高层类也没有直接提供,只能靠自己用低层类来构造, 所以哪怕在一些著名的著作中,都很晚才讲到输入输出流,因为过早的 谈这个,反而使大家迷惑,比如《Core Java2原理》,书中一直是用 一个输入对话框(JOptionPane)来让大家输入数据的,而到了最后,提 到输入输出流时,才开始谈及。System.in.read()派生于输入流类InputStream,而read()方法的 原型是这样的:abstract int read() JDK是这样描述这个方法的: Reads the next byte of data from the input stream. 就是读入数据流中的下一个字节。int n=System.in.read(); 读入的就是字节流,返回的int就是一个字符的ASCII码的十进制形式, 所以比如输入3或者315或者30000等等,n永远是字符3的ASCII码,即51。如果一定要用System.in.read(...)方法,那么InputStream还有两个read 方法: int read(byte[] b) //Reads some number of bytes from the input stream and stores them into the buffer array b. int read(byte[] b, int off, int len) //Reads up to len bytes of data from the input stream into an array of bytes. 可以这样用: byte[] bin=new byte[80]; int len=System.in.read(bin,0,80); //len是实际读入的长度 String strin=new String(bin,0,len-2); //len-2是,是因为末两个字节的数据其实是回车换行符,可以忽略, //当然不减去,也是可以的,待会儿转换函数可以自动处理掉。 int x=Integer.parseInt(strin); System.out.println("x="+x);当然,还有很多方法可以接受键盘输入的数据,可以用 很多流类来构造,不过或许BufferedReader来包装的似乎 最常见,也比较安全吧。 附一个实例 //AA.java import java.io.*; public class AA { public static void main(String agrs[]) throws IOException {
byte[] bin=new byte[80]; int len=System.in.read(bin,0,80); String strin=new String(bin,0,len-2); try { int x=Integer.parseInt(strin); System.out.println("x="+x); }catch(NumberFormatException e) { System.out.println("您输入的数据是非法的,无法转化成整数"); } } } */对于xiaoshou() 提到的问题,呵呵,仔细观察编译信息! System.in.read()确实是在java.io包中,所以要先import 这勿庸置疑,之所以要try{}catch(){},你看看编译提示的 信息:“unreported exception java.io.IOException; must be caught or declared to be thrown ”——异常必须被捕捉,或者抛出。这正是JAVA为了安全的体现, 它是强制性的,因为输入操作经常会发生异常,所以一定要处理 可能抛出的异常,最简单的,你可以不用catch来处理,可以在 你这个调用read()方法的函数声明后加上throws IOException来 抛出异常。
System.out.println(max);
System.out.println(min);
放入main()函数。
另外把函数体放入try{ }catch(IOException e){ }中。
main()-> main(String[] args)类中只能有方法和数据成员。System的主要方法(函数)有 :
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
public static String getProperty(String key, String def) public static String getProperty(String key) public static void loadLibrary(String libname) public static void setProperties(Properties props) public static String setProperty(String key, String value) public static void setOut(PrintStream out) public static native long currentTimeMillis(); public static void setIn(InputStream in)
public static void main(string agrs[])
{ int max,min;
int a[]=new int[10];
try{
a[0]=System.in.read( );
max=a[0];
min=a[0];
for(int i=1;i<10;i++)
{
a[i]=System.in.read( );
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
}catch(IOException e){ }
System.out.println(max);
System.out.println(min);
}
}
System.out.println(min);
那么输出的格式是怎样的啊!我只知道怎样输出字符串。例如要输出整型数。就像我
的代吗中吗??但有错啊??是不是与C中一样的啊???
{
语句1
}
catch(Exception1 e)
{
语句2
}
catch(Exception2 e)
{
语句3
}所谓try{}catch(){}简单来讲就是捕捉异常,以上面例子的格式,语句1是你要执行的一段语句,这段语句可能发生异常,try{}catch(){}要捕捉的异常就来自语句1中,语句2和语句3分别是发生了Exception1 和Exception2时你想要执行的语句,catch可以有很多个,Exception也可有很多分类,其中java本身定义了不少Exception,另外你也可以自己定义自己的Exception!!
(IOException e)的括号是全角的吧
还有就是请明说java报了啥错
--------------------Configuration: j2sdk1.4.2 <Default>--------------------
C:\Documents and Settings\Administrator\桌面\Maxmin.java:8: unreported exception java.io.IOException; must be caught or declared to be thrown
a[0]=System.in.read( );
^
C:\Documents and Settings\Administrator\桌面\Maxmin.java:13: unreported exception java.io.IOException; must be caught or declared to be thrown
a[i]=System.in.read( );
我也搞不明白,期待高手的解答。
^
public class Test
{
public static void main(String agrs[])
{
int max,min;
int a[]=new int[10];
try{
a[0]=System.in.read( );
max=a[0];
min=a[0];
for(int i=1;i<10;i++)
{
a[i]=System.in.read( );
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
System.out.println(max);
System.out.println(min);
}
}catch(IOException e){ }
}
}
System.in.read(),这怎么会行呢?程序能运行也不能
得到正确解答!
正解如下:
import java.io.*;
public class Test
{
public static void main(String agrs[])
{
int max=0,min=0;
int a[]=new int[10];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
a[0]=Integer.parseInt(br.readLine());
max=a[0];
min=a[0];
for(int i=1;i<10;i++)
{
a[i]=Integer.parseInt(br.readLine());
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
}catch(IOException e){ }
System.out.println(max);
System.out.println(min);
}
}
樓主,你的max=System.in.read() ;
min=System.in.read() ;
有問題,這個方法不是這樣用的.應該System.in.read(String str)
这个方法吗????麻烦你查查JAVA-DOC再说的,先!!!
用System.in.read()返回的是Ascll码,如:1->49,如何把他转换成
数值呢??
只要import java.io.*;应该可以!但是不行!
为什么得在public static void main() throws java.io.IOException{
}或者try{}catch{}里面才编译通过?:)
呵呵,其实用BufferedReader来包装一个System.in在JAVA中真的算是
最简单的了,大家不能将JAVA的输入输出流与C++中的输入输出流来看,
因为JAVA中关于输入输出流,大大小小的类,真的是远比C++多,而且
提供的流类大多是一些低层的类,哪怕要最简单的从键盘输入一个整数
这样的直接的高层类也没有直接提供,只能靠自己用低层类来构造,
所以哪怕在一些著名的著作中,都很晚才讲到输入输出流,因为过早的
谈这个,反而使大家迷惑,比如《Core Java2原理》,书中一直是用
一个输入对话框(JOptionPane)来让大家输入数据的,而到了最后,提
到输入输出流时,才开始谈及。System.in.read()派生于输入流类InputStream,而read()方法的
原型是这样的:abstract int read()
JDK是这样描述这个方法的:
Reads the next byte of data from the input stream.
就是读入数据流中的下一个字节。int n=System.in.read();
读入的就是字节流,返回的int就是一个字符的ASCII码的十进制形式,
所以比如输入3或者315或者30000等等,n永远是字符3的ASCII码,即51。如果一定要用System.in.read(...)方法,那么InputStream还有两个read
方法:
int read(byte[] b)
//Reads some number of bytes from the input stream and stores them into the buffer array b.
int read(byte[] b, int off, int len)
//Reads up to len bytes of data from the input stream into an array of bytes. 可以这样用:
byte[] bin=new byte[80];
int len=System.in.read(bin,0,80);
//len是实际读入的长度
String strin=new String(bin,0,len-2);
//len-2是,是因为末两个字节的数据其实是回车换行符,可以忽略,
//当然不减去,也是可以的,待会儿转换函数可以自动处理掉。
int x=Integer.parseInt(strin);
System.out.println("x="+x);当然,还有很多方法可以接受键盘输入的数据,可以用
很多流类来构造,不过或许BufferedReader来包装的似乎
最常见,也比较安全吧。
附一个实例
//AA.java
import java.io.*;
public class AA
{
public static void main(String agrs[]) throws IOException
{
byte[] bin=new byte[80];
int len=System.in.read(bin,0,80);
String strin=new String(bin,0,len-2);
try
{
int x=Integer.parseInt(strin);
System.out.println("x="+x);
}catch(NumberFormatException e)
{
System.out.println("您输入的数据是非法的,无法转化成整数");
}
}
}
*/对于xiaoshou() 提到的问题,呵呵,仔细观察编译信息!
System.in.read()确实是在java.io包中,所以要先import
这勿庸置疑,之所以要try{}catch(){},你看看编译提示的
信息:“unreported exception java.io.IOException;
must be caught or declared to be thrown
”——异常必须被捕捉,或者抛出。这正是JAVA为了安全的体现,
它是强制性的,因为输入操作经常会发生异常,所以一定要处理
可能抛出的异常,最简单的,你可以不用catch来处理,可以在
你这个调用read()方法的函数声明后加上throws IOException来
抛出异常。
明白了很多!!
先不结帖子,让更多的人进来看看,让大家学习学习!!