自己寫的,基礎(chǔ)知識掌握的很差,大神幫忙看看
package didizuche;
import java.util.*;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("歡迎使用滴滴租車系統(tǒng)"+"\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("請輸入你要租汽車的數(shù)量");
? ? ? ? ? ? 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("請輸入要租的天數(shù):");
? ? ? ? ? ? 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" ,應(yīng)該賦值為"",這樣在拼接車名字的時候不會出現(xiàn)null了;
if (c == 1) {
allpeople += 4;
allthing += 0;
allprice += 500;
Person += "奧迪A4";
}
車的屬性到成員變量了這里就不用手動加 常量了。直接加對象的成員變量就好了;
2016-06-21
2016-06-21
//以奧迪為例
public static class Aodi extends Car {
String name = "奧迪A4"; //車的名字
int people = 4; //容納人數(shù)
int thing = 0; //容納貨物數(shù)量
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();
}