自己寫的,基礎知識掌握的很差,大神幫忙看看
package didizuche;
import java.util.*;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("歡迎使用滴滴租車系統"+"\n"+"您是否要租車:1是 0否");
Scanner judge=new Scanner(System.in);
int a=judge.nextInt();
if(a==1){
System.out.println("您可租車的類型及價目表"+"\n"+"序號"+"\t"+"汽車名稱"+"\t"+"租金"+"\t"+"容量");
? ? ? ? ? ? Car test1=new Aodi();test1.car();
? ? ? ? ? ? Car test2=new Mazida();test2.car();
? ? ? ? ? ? Car test3=new Pikaxue();test3.car();
? ? ? ? ? ? Car test4=new Jinlong();test4.car();
? ? ? ? ? ? Car test5=new Songhuajia();test5.car();
? ? ? ? ? ? Car test6=new Yiweike();test6.car();
? ? ? ? ? ? System.out.println("請輸入你要租汽車的數量");
? ? ? ? ? ? Scanner amount=new Scanner(System.in);
? ? ? ? ? ? int b=amount.nextInt();
? ? ? ? ? ? int c;int allpeople=0;int allthing=0;int allprice=0;String Person=null;String Cars=null;
? ? ? ? ? ? for(int i=1;i<=b;i++){ ? ? ? ? ?
? ? ? ? ? ? do{
? ? ? ? ? ? System.out.println("請輸入第"+i+"輛車的序號:");
? ? ? ? ? ? ? ?Scanner xuhao=new Scanner(System.in);
? ? ? ? ? ? ? ?c=xuhao.nextInt();
? ? ? ? ? ? }while(c<1||c>6);
? ? ? ? ? ? if(c==1){
? ? ? ? ? ? allpeople+=4;allthing+=0;allprice+=500;Person+="奧迪A4";
? ? ? ? ? ? }else if(c==2){
? ? ? ? ? ? allpeople+=4;allprice+=400;Person+="馬自達6";
? ? ? ? ? ? }else if(c==3){
? ? ? ? ? ? allpeople+=4;allthing+=2;allprice+=450;Person+="皮卡雪6";Cars+="皮卡雪6";
? ? ? ? ? ? }else if(c==4){
? ? ? ? ? ? allpeople+=20;;allprice+=800;Person+="金龍";
? ? ? ? ? ? }else if(c==5){
? ? ? ? ? ? allthing=4;allprice+=400;Cars+="松花江";
? ? ? ? ? ? }else{
? ? ? ? ? ? allthing+=20;allprice+=1000;Cars+="依維柯";
? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println("請輸入要租的天數:");
? ? ? ? ? ? Scanner days=new Scanner(System.in);
? ? ? ? ? ? int d=days.nextInt();
? ? ? ? ? ? System.out.println("你的賬單"+"\n"+"***可載人的車有:"+"\n"+Person+"\t"+"共載人:"+allpeople+"人");
? ? ? ? ? ? System.out.println("***載貨的車有:"+"\n"+Cars+"\t"+"共載貨:"+allthing+"噸");
? ? ? ? ? ? System.out.println("***租車總價格:"+allprice*d+"元");
}
? ? ? ??
}
}
2016-06-20
String Person = null;?String Cars=null; ? Cars賦值null ,在拼接字符串的時候 就是 "null" ,應該賦值為"",這樣在拼接車名字的時候不會出現null了;
if (c == 1) {
allpeople += 4;
allthing += 0;
allprice += 500;
Person += "奧迪A4";
}
車的屬性到成員變量了這里就不用手動加 常量了。直接加對象的成員變量就好了;
2016-06-21
import?java.util.*; public?class?Test?{ public?static?void?main(String[]?args)?{ //所有車放在這里,可以根據序號取對應的車 List<Car>?cars?=?new?ArrayList<Car>(); Car?aodi?=?new?Aodi(); cars.add(aodi); Car?mazida?=?new?Mazida(); cars.add(mazida); Car?pikaxue?=?new?Pikaxue(); cars.add(pikaxue); Car?jinlong?=?new?Jinlong(); cars.add(jinlong); Car?songhuajia?=?new?Songhuajia(); cars.add(songhuajia); Car?yiweike?=?new?Yiweike(); cars.add(yiweike); System.out.println("歡迎使用滴滴租車系統"?+?"\n"?+?"您是否要租車:1是?0否"); Scanner?judge?=?new?Scanner(System.in); int?a?=?judge.nextInt(); if?(a?==?1)?{ System.out.println("您可租車的類型及價目表"?+?"\n"?+?"序號"?+?"\t"?+?"汽車名稱"?+?"\t"?+?"租金"?+?"\t"?+?"容量"); aodi.car(1); mazida.car(2); pikaxue.car(3); jinlong.car(4); songhuajia.car(5); yiweike.car(6); ????????????System.out.println("請輸入你要租汽車的數量"); ????????????Scanner?amount=new?Scanner(System.in); ????????????int?b=amount.nextInt(); int?c; int?allpeople?=?0; int?allthing?=?0; int?allprice?=?0; String?Person?=?""; String?Cars?=?""; ????????????for(int?i=1;i<=b;i++){?????????? do?{ System.out.println("請輸入第"?+?i?+?"輛車的序號:"); Scanner?xuhao?=?new?Scanner(System.in); c?=?xuhao.nextInt(); }while(c<1||c>6); //根據序號在List中取相應的車 Car?cc?=?cars.get(c-1); //如果該車能裝人 if(cc.people>0){ allpeople+=cc.people; Person?+=?cc.name+"?"; } //如果該車能裝貨 if(cc.thing>0){ allthing+=cc.thing; Cars?+=?cc.name+"?"; } allprice?+=?cc.price; ????????????} ????????????System.out.println("請輸入要租的天數:"); ????????????Scanner?days=new?Scanner(System.in); ????????????int?d=days.nextInt(); ????????????System.out.println("你的賬單"+"\n"+"***可載人的車有:"+"\n"+Person+"\t"+"共載人:"+allpeople+"人"); ????????????System.out.println("***載貨的車有:"+"\n"+Cars+"\t"+"共載貨:"+allthing+"噸"); ????????????System.out.println("***租車總價格:"+allprice*d+"元"); } } } class?Car{ String?name?=?""; int?people?=?0; int?thing?=?0; float?price?=?0; public?void?car(int?index){ System.out.println(index?+?"\t"?+?name?+?"\t"?+?price?+?"\t"?+?people?+?"人"?+?thing?+?"貨"); } } class?Aodi?extends?Car{{ name?=?"奧迪A4"; people?=?4; thing?=?0; price?=?500; }} class?Mazida?extends?Car{{ name?=?"馬自達6"; people?=?4; thing?=?0; price?=?400; }} class?Pikaxue?extends?Car{{ name?=?"皮卡雪6"; people?=?4; thing?=?2; price?=?450; }} class?Jinlong?extends?Car{{ name?=?"金龍"; people?=?20; thing?=?0; price?=?800; }} class?Songhuajia?extends?Car{{ name?=?"松花江"; people?=?0; thing?=?4; price?=?400; }} class?Yiweike?extends?Car{{ name?=?"依維柯"; people?=?20; thing?=?0; price?=?1000; }}2016-06-21
//以奧迪為例
public static class Aodi extends Car {
String name = "奧迪A4"; //車的名字
int people = 4; //容納人數
int thing = 0; //容納貨物數量
float price = 500; //價格
@Override
public void car() {
System.out.println("1" + "\t" + name + "\t" + price + "\t" + people + "人" + thing + "貨");
}
}
這樣在選車的時候 ?就可以寫成
if (c == 1) {
allpeople += ((Aodi)test1).getPeople();
allthing += ((Aodi)test1).getThing();
allprice += ((Aodi)test1).getPrice();
Person += ((Aodi)test1).getName();
}