import java.io.*;
class book{
           String name="三国演义";
           String author="罗贯中";
           String publishingHouse="商务出版社";
           double price=39.9;
           String date="2006/08/23"; 
           void show(){
           System.out.println("the books' name is"+name);
           System.out.println("作者:"+author);
           System.out.println("出版商:"+publishingHouse);
           System.out.println("价格:"+price);
           System.out.println("出版日期:"+date);}}
class CD{
           String name="三十";
           String singer="羽泉";
           String company="华娱";
           double price=30;
           String date="2005/09/01";
           void show(){ 
           System.out.println("CD名:"+name);
           System.out.println("歌手:"+singer);
           System.out.println("发行商:"+company);
           System.out.println("价格:"+price);
           System.out.println("发行日期:"+date);}}
class disk{
           String name="三十";
           String singer="羽泉";
           String company="华娱";
           double price=30;
           String date="2005/09/01";
           void show(){ 
           System.out.println("CD名:"+name);
           System.out.println("歌手:"+singer);
           System.out.println("发行商:"+company);
           System.out.println("价格:"+price);
           System.out.println("发行日期:"+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("Input a classname,用1代替book,2代替CD,3代替disk");  主要是
          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("wrong data!"); }
          catch(IOException e){System.err.println();}                             */
          
          
       }
    }
上边都是次要的,主要是从try开始,那地方有错误,自己现在还没弄明白

解决方案 »

  1.   


    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 = "";
    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>>i这种的,java中的输入输出略显麻烦,不过用久自然也习惯了
      

  2.   

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

  3.   

    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("请输入要调用方法序号:\n 1:b.show()\n 2:c.show()\n 3:d.show()");
    }else{
    for (int i=0; i < args.length; i++) {
    int arg = Integer.parseInt(args[i]);
    switch(arg){
    //------------------------------
    case 1:{
    System.out.println("b.show()"+b.show());
    }
    break;
    //-------------------------------
    case 2:{
    System.out.println("c.show()"+c.show());
    }
    break;
    //-------------------------
    case 3:{
    System.out.println("d.show()"+d.show());
    }
    break;
    }
    }
    }
    }
      

  4.   

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

  5.   

    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("Input a classname,用1代替book,2代替CD,3代替disk");  
              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("wrong data!"); }
              catch(IOException e){System.err.println();}                             
              
              
           }
        }