Scanner工具類的初始化問題,求大神
//父類
package text2;
public class vehicle {
? ? ? ?public void transport(){
? ? ? System.out.println("交通工具具有運(yùn)輸?shù)哪芰?);
? ? ? ?}
? ? ? ?public int site;//載客數(shù)
? ? ? ?public String transMode;//運(yùn)送方式
? ? ??
? ? ? ?public void operation(){
? ? ? ? ? ?
? ? ? ?}
}
//子類
package text2;
public class car extends vehicle {
int site=1;
? ? String transMode="land";
? ? public void operation(){
? ? ? ? System.out.println("汽車可載客"+site+"人,運(yùn)送方式為"+transMode);
? ? ? ? ? ? ? ? ?
? ? }
}
//子類
package text2;
public class plane extends vehicle {
int site=150;
? ? String transMode="fly";
? ? public void operation(){
? ? ? ? System.out.println("飛機(jī)可載客"+site+"人,運(yùn)送方式為"+transMode);
? ? ? ? ? ? ? ? ?
? ? }
}
//子類
package text2;
public class train extends vehicle {
int site=1500;
? ? String transMode="land";
? ? public void operation(){
? ? ? ? System.out.println("火車可載客"+site+"人,運(yùn)送方式為"+transMode);
? ? ? ? ? ? ? ? ?
? ? }
}
//方法
package text2;
import java.util.Scanner;
public class inital {
public static void main(String[] args) {
// TODO Auto-generated method stub
? ? vehicle car=new car();
? ? vehicle plane=new plane();
? ? vehicle train=new train();
? ? Scanner input =new Scanner(System.in);
? ? System.out.println("請(qǐng)輸入查詢的對(duì)象:");
? ? String Name=input.next();
? ? String a="plane";
? ? String b="train";
? ? String c="car";
? ? if(Name.equals(a)){
? ? plane.operation();
? ? }else if(Name.equals(b)){
? ? train.operation();
? ? }else if(Name.equals(c)){
? ? car.operation();
? ? }else{
? ? ? ? System.out.println("!請(qǐng)檢查輸入的對(duì)象名稱");
? ? }
? ? input.close();
}
}
問題就是每次執(zhí)行的時(shí)候只能查詢一次,輸錯(cuò)了回車執(zhí)行之后也不能重新輸入,怎么樣可以多次查詢啊
2016-09-01
只弄了while那里的代碼,我自己是了是可以的,你可以改一下
2016-08-11
)
2016-08-04
while (true) {
int s = input.nextInt();
System.out.println(s);
if (s == 0) {//循環(huán)終止條件
break;
}
}
input.close();
2016-07-25
while(input.hasNext){主代碼段} ? ?可行否? ?