就是關(guān)于JAVA入門第二季最后那個(gè)綜合練習(xí)題,請(qǐng)問(wèn)最后怎樣可以實(shí)現(xiàn)對(duì)已經(jīng)選擇了的車輛name的輸出?以下是自己寫的程序
(父類) package?imooc1; public?class?car?{ ????public??String?number; ????public??String?name; ????public??int????people; ????public??int????weight; ????public??int????price; ??? /*生產(chǎn)setter和getter方法*/ public?String?getNumber()?{ return?number; } public?void?setNumber(String?number)?{ this.number?=?number; } public?String?getName()?{ return?name; } public?void?setName(String?name)?{ this.name?=?name; } public?int?getPeople()?{ return?people; } public?void?setPeople(int?people)?{ this.people?=?people; } public?int?getWeight()?{ return?weight; } public?void?setWeight(int?weight)?{ this.weight?=?weight; } public?int?getPrice()?{ return?price; } public?void?setPrice(int?price)?{ this.price?=?price; } ???? } (子類) package?imooc1; import?java.util.Scanner; public?class?rental?extends?car?{ public?static?void?main(String[]?args){ System.out.println("歡迎使用達(dá)達(dá)租車系統(tǒng):"); System.out.println("您是否要租車:1是0否"); /*創(chuàng)建Scanner對(duì)象,用戶輸入數(shù)據(jù)*/ Scanner?input=new?Scanner(System.in); /*獲取用戶輸入的信息*/ int?wh=input.nextInt(); if(wh==1){ /*調(diào)用并輸出6個(gè)方法可供選擇,并輸出6個(gè)對(duì)象的全部信息*/ rental?car1=new?rental(); ?car1.aud(); rental?car2=new?rental();? ?car2.maz(); rental?car3=new?rental(); ?car3.pick(); rental?car4=new?rental(); ?car4.jinl(); rental?car5=new?rental();? ?car5.sonha(); ?rental?car6=new?rental(); car6.yiwek(); System.out.println("請(qǐng)輸入您要租的車的數(shù)量:"); } /*輸出客戶選擇的汽車數(shù)量*/ Scanner?input1=new?Scanner(System.in); /*獲取用戶輸入的信息*/ int?wc=input1.nextInt(); double?count=0; double?wei=0; int?peo=0; //String?har; for(int?i=1;i<=wc;i++){ System.out.println("請(qǐng)輸入第"+i+"輛車的序號(hào):"); Scanner?input2=new?Scanner(System.in); int?w=input2.nextInt();//輸入選擇的車序號(hào) switch(w){ ????? case?1:count+=500;peo+=4;break;//奧迪A4 case?2:count+=400;peo+=4;break;//馬自達(dá)6 case?3:count+=450;wei+=2;break;//皮卡雪6 case?4:count+=800;peo+=20;break;//金龍 case?5:count+=400;wei+=4;break;//松花江 case?6: count+=1000;wei+=20;//依維柯 } } System.out.println("請(qǐng)輸入租車天數(shù):"); Scanner?input2=new?Scanner(System.in); int?w=input2.nextInt();//輸入租用天數(shù) ?count=?count*wc;//所有租車費(fèi)用 System.out.println("您的賬單:"); System.out.println("可載人的車有:"); System.out.println("總載人數(shù):"+peo); System.out.println("可載貨的車有:"); System.out.println("總載貨:"+wei+"噸"); System.out.println("租車總費(fèi)用為"+count); } /*創(chuàng)建6個(gè)車型對(duì)象方法*/ public?void?aud(){ ????this.number="1";????? this.price=500; this.name="奧迪A4"; this.people=4; System.out.println("序號(hào)"+number+"???車型"+name+"???價(jià)格"+price+"元/天???????載物量"+people+"人"); } public?void?maz(){ this.number="2";????? this.price=400; this.name="馬自達(dá)6"; this.people=4; System.out.println("序號(hào)"+number+"???車型"+name+"???價(jià)格"+price+"元/天???????載物量"+people+"人"); } public?void?pick(){ this.number="3";????? this.price=450; this.name="皮卡雪6"; this.people=4; this.weight=6; System.out.println("序號(hào)"+number+"???車型"+name+"????價(jià)格"+price+"元/天????????載物量"+people+"人?/"+weight+"噸"); } public?void?jinl(){ this.number="4";????? this.price=800; this.name="金龍"; this.people=20; System.out.println("序號(hào)"+number+"???車型"+name+"???價(jià)格"+price+"元/天???????載物量"+people+"人"); } public?void?sonha(){ this.number="5";????? this.price=400; this.name="松花江"; this.weight=4; System.out.println("序號(hào)"+number+"???車型"+name+"???價(jià)格"+price+"元/天???????載物量"+weight+"噸"); } public?void?yiwek(){ this.number="6";????? this.price=1000; this.name="依維柯"; this.weight=20; System.out.println("序號(hào)"+number+"???車型"+name+"???價(jià)格"+price+"元/天???????載物量"+weight+"噸"); } }
怎樣在switch語(yǔ)句中收集已選擇了的汽車車型名字?當(dāng)局者迷,求老師指點(diǎn)。。。。。。。Iam waiting?
2014-11-20
你可以定義一個(gè)字符串,然后在switch中將車的名字拼接在這個(gè)字符串中