站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题刚入门者的上机题,在异常方面有些问题,请大家指教

刚入门者的上机题,在异常方面有些问题,请大家指教

摘要: 刚入门者的上机题,在异常方面有些问题,请大家指教


import java.io.*;
class book{
String name=-#34;三国演义-#34;;
String author=-#34;罗贯中-#34;;
String publishingHouse=-#34;商务出版社-#34;;
double price=39.9;
String date=-#34;2006/08/23-#34;;
void show(){
System.out.println(-#34;the books-#39; name is-#34;+name);
System.out.println(-#34;作者:-#34;+author);
System.out.println(-#34;出版商:-#34;+publishingHouse);
System.out.println(-#34;价格:-#34;+price);
System.out.println(-#34;出版日期:-#34;+date);}}
class CD{
String name=-#34;三十-#34;;
String singer=-#34;羽泉-#34;;
String company=-#34;华娱-#34;;
double price=30;
String date=-#34;2005/09/01-#34;;
void show(){
System.out.println(-#34;CD名:-#34;+name);
System.out.println(-#34;歌手:-#34;+singer);
System.out.println(-#34;发行商:-#34;+company);
System.out.println(-#34;价格:-#34;+price);
System.out.println(-#34;发行日期:-#34;+date);}}
class disk{
String name=-#34;三十-#34;;
String singer=-#34;羽泉-#34;;
String company=-#34;华娱-#34;;
double price=30;
String date=-#34;2005/09/01-#34;;
void show(){
System.out.println(-#34;CD名:-#34;+name);
System.out.println(-#34;歌手:-#34;+singer);
System.out.println(-#34;发行商:-#34;+company);
System.out.println(-#34;价格:-#34;+price);
System.out.println(-#34;发行日期:-#34;+date);}}
public class chose{
static book b;static CD c;static disk d;
public static void main(String args[]){
int n=0;
try{ /*我
System.out.println(-#34;Input a classname,用1代替book,2代替CD,3代替disk-#34;); 主要是
n=System.in.read(); 对输入
if(n==1) b.show(); 有些不
else if(n==2) c.show(); 理解
else if(n==3) d.show();
else System.out.println(-#34;wrong data!-#34;); }
catch(IOException e){System.err.println();} */


}
}
上边都是次要的,主要是从try开始,那地方有错误,自己现在还没弄明白



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Input {
public static void main(String[] args){
try{
BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
//System.out.println(System.in.read());
String line = -#34;-#34;;
int inputInt =-1;
while(line!=null){
line = in.readLine() ;
inputInt = Integer.parseInt( line);
System.out.println(inputInt);
}
}catch(IOException ioe){
ioe.printStackTrace() ;

}


}

}

-----------------------
java 中读标准输入(键盘)中读取数据,一般是先弄一个bufferedReader(因为有readLine,当然其他的reader或者inputStream也可以,只不过个人觉得readLine稍微方便一点,当然你也可以用最原始的System.in.read()只不过这个read读过来的是输入的ascii字符的ascii值,还要自己转换
把字符读进来之后再用Integer.parseInt方法转换成int或者Integer,或者Long.parseLong.,等等对于习惯了c++中int i;cin-#62;-#62;i这种的,java中的输入输出略显麻烦,不过用久自然也习惯了


用io输入的东西是字符串
可能不是你想要的东西


System.in.read(),每次读一个字符,返回int,其值是该字符的ascii码


public static void main(String[] args)
{
book b = new book();
CD c = new CD();
disk d = new disk();

if(args.length == 0) {
System.out.println(-#34;请输入要调用方法序号:/n 1:b.show()/n 2:c.show()/n 3:d.show()-#34;);
}else{
for (int i=0; i -#60; args.length; i++) {
int arg = Integer.parseInt(args[i]);
switch(arg){
//------------------------------
case 1:{
System.out.println(-#34;b.show()-#34;+b.show());
}
break;
//-------------------------------
case 2:{
System.out.println(-#34;c.show()-#34;+c.show());
}
break;
//-------------------------
case 3:{
System.out.println(-#34;d.show()-#34;+d.show());
}
break;
}
}
}
}


我说那个叫什么 十五维 的,你调用对象实例化了没有啊 ?
鄙视!!


n=System.in.read();
你应该只try这句
并且改为
n=(int)System.in.read();


System.in.read() 这句是把从键盘上读入的字符放入一个整型值里面,比如输入1,那么你这个n的值就是49了,所以n这个变量里面的值是49,这是ascII码值,那么至于你的空指值异常是因为
static book b;static CD c;static disk d;这一句没有实例化对象,不能直接拿b,c,d这几个引用变量来使用,
这样改就可以了:
public class Test{
static book b = new book() ;
static CD c = new CD();
static disk d = new disk();
public static void main(String args[]){
int n=0;
try{
System.out.println(-#34;Input a classname,用1代替book,2代替CD,3代替disk-#34;);
n=(int)System.in.read();
if(n==48) b.show();
else if(n==49) c.show();
else if(n==50) d.show();
else System.out.println(-#34;wrong data!-#34;); }
catch(IOException e){System.err.println();}


}
}


↑返回目录
前一篇: 请java高手,来帮我解决一下这个问题!一定结帖.
后一篇: [说难不难的问题]:__FUNCTION__是c++中表示当前函数名的宏,java中又如何 获取当前类和方法 ?