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开始,那地方有错误,自己现在还没弄明白
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开始,那地方有错误,自己现在还没弄明白
解决方案 »
- 参数 tcp_DEFAULT_IP 的修饰符不合法;只允许使用终态
- JTable用AbstractTableModel怎样添加、删除数据
- 新建文件报错问题
- JAVA新手遇到的程序问题
- java 返回值为什么为常量好
- 正则表达式的含义是什么啊`` 它该怎么用啊``` 望高手能给详细解释一下```谢谢呢``
- 如何移动另一程序的窗口到指定位置
- 我是新学的,遇到个问题,有人能帮我吗?感谢~
- 关于如何使用package的简单问题
- net help
- [说难不难的问题]:__FUNCTION__是c++中表示当前函数名的宏,java中又如何 获取当前类和方法 ?
- 100 分求助 ! 在JAVA 中如何获取一个对象占用的实际内存大小?
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中的输入输出略显麻烦,不过用久自然也习惯了
{
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;
}
}
}
}
你应该只try这句
并且改为
n=(int)System.in.read();
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();}
}
}