Scanner工具類的初始化問題,求大神
//父類
package text2;
public class vehicle {
? ? ? ?public void transport(){
? ? ? System.out.println("交通工具具有運輸的能力");
? ? ? ?}
? ? ? ?public int site;//載客數
? ? ? ?public String transMode;//運送方式
? ? ??
? ? ? ?public void operation(){
? ? ? ? ? ?
? ? ? ?}
}
//子類
package text2;
public class car extends vehicle {
int site=1;
? ? String transMode="land";
? ? public void operation(){
? ? ? ? System.out.println("汽車可載客"+site+"人,運送方式為"+transMode);
? ? ? ? ? ? ? ? ?
? ? }
}
//子類
package text2;
public class plane extends vehicle {
int site=150;
? ? String transMode="fly";
? ? public void operation(){
? ? ? ? System.out.println("飛機可載客"+site+"人,運送方式為"+transMode);
? ? ? ? ? ? ? ? ?
? ? }
}
//子類
package text2;
public class train extends vehicle {
int site=1500;
? ? String transMode="land";
? ? public void operation(){
? ? ? ? System.out.println("火車可載客"+site+"人,運送方式為"+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("請輸入查詢的對象:");
? ? 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("!請檢查輸入的對象名稱");
? ? }
? ? input.close();
}
}
問題就是每次執行的時候只能查詢一次,輸錯了回車執行之后也不能重新輸入,怎么樣可以多次查詢啊
2016-09-01
只弄了while那里的代碼,我自己是了是可以的,你可以改一下
2016-08-11
)
2016-08-04
while (true) {
int s = input.nextInt();
System.out.println(s);
if (s == 0) {//循環終止條件
break;
}
}
input.close();
2016-07-25
while(input.hasNext){主代碼段} ? ?可行否? ?