问题程序,请求指点!
import *;import java.util.Scanner;
public class sy2_2 {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Tool tool;
tool=new Tool("剪刀",2000);
tool.print();
tool.set();
tool.print();
House house;
house=new House("普通房子",20,10,3);
house.print();
house.set();
house.print();
}
}
class Tool{
String name;
int date;
Tool(String name,int data){
this.name=name;
this.date=data;
}
void print(){
System.out.println("名称:"+name);
System.out.println("制造年份:"+date);
}
void set(){
int choice;
Scanner read=new Scanner(System.in);
System.out.print("请选择要设置的属性(1-名称/2-制造年份):");
choice=new Scanner(System.in).nextInt();
if(choice==1){
System.out.print("名称:");
read.next(name);
}
else{
System.out.print("制造年份:");
read.nextInt(date);
}
}
}
class House{
String name;
int length,width,high;
House(String name,int length,int width,int high){
this.name=name;
this.length=length;
this.width=width;
this.high=high;
}
void print(){
System.out.println("名称:"+name);
System.out.println("长度:"+length);
System.out.println("宽度:"+width);
System.out.println("高度:"+high);
}
void set() throws IOException {
char choice;
Scanner read=new Scanner(System.in);
System.out.println("请选择要设置的属性(n-名称/l-长度/w-宽度/h-高度):");
choice=(char)System.in.read();
switch(choice){
case 'n':{System.out.print("名称:");read.next(name);break;}
case 'l':{System.out.print("长度:");read.nextInt(length);break;}
case 'w':{System.out.print("宽度:");read.nextInt(width);break;}
case 'h':{System.out.print("高度:");read.nextInt(high);break;}
default:System.out.println("输入错误!");
}
}
}
运行之后显示以下内容:
名称:剪刀
制造年份:2000
请选择要设置的属性(1-名称/2-制造年份):1
名称:锤子
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Tool.set(sy2_2.java:41)
at sy2_2.main(sy2_2.java:12)
我是新手,所以不太看懂,还请各位能人出手相助,不胜感激!