面向?qū)ο蟮牧?xí)題
package demo.faceobj;
import java.util.*;
public class test {
?? ?public static void main(String[] args) {
?? ??? ?int pSum=0;//總?cè)藬?shù)
?? ??? ?int oSum=0;//總貨物量
?? ??? ?double sumMon=0;//總金額
?? ??? ?Car[] crent={new peCar("奧迪",500,4),new peCar("馬自達(dá)",400,4),new pickCar("皮卡",450,4,2),new
?? ??? ??? ??? ?peCar("金龍",800,20),new obCar("松花江",400,4),new obCar("依維河",1000,20)};
?? ??? ?System.out.println("歡迎使用租車系統(tǒng):");
?? ??? ?System.out.println("您是否需要租車:1是 0否");
?? ??? ?Scanner input=new Scanner(System.in);
?? ??? ?int n=input.nextInt();
?? ??? ?if(n==1){
?? ??? ??? ?System.out.println("您可租車的類型及其價目表:");
?? ??? ??? ?System.out.println("序號"+"\t"+"汽車名稱"+"\t"+"租金"+"\t"+"容量");
?? ??? ??? ?for(int i=0;i<crent.length;i++){
?? ??? ??? ??? ?System.out.print(i+1+".\t");
?? ??? ??? ??? ?crent[i].print();
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?System.out.println("請輸入要租車的數(shù)量:");
?? ??? ??? ?int num=input.nextInt();
?? ??? ??? ?int [] x=new int[num];//用以保存車序的數(shù)組
?? ??? ??? ?for(int i=0;i<num;i++){
?? ??? ??? ??? ?System.out.println("請輸入第"+(i+1)+"量車的序號:");
?? ??? ??? ??? ?int Cnum=input.nextInt();
?? ??? ??? ??? ?int innum=Cnum-1;
?? ??? ??? ??? ?x[i]=innum;?? ?
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?System.out.println("請輸入租車天數(shù):");
?? ??? ??? ?int Day=input.nextInt();
?? ??? ??? ?
?? ??? ??? ?System.out.println("您的賬單:");
?? ??? ??? ?System.out.println("***可載人的車有:");
?? ??? ??? ?for(int i=0;i<x.length;i++){
?? ??? ??? ??? ?if(x[i]==0||x[i]==1||x[i]==2||x[i]==3){
?? ??? ??? ??? ??? ?System.out.print(crent[x[i]].name+"\t");
?? ??? ??? ??? ??? ?pSum+=crent[x[i]].pnum;
?? ??? ??? ??? ??? ?sumMon+=crent[x[i]].mon;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?System.out.println("共載人:"+pSum+"人");
?? ??? ??? ?System.out.println("***可載貨的車有:");
?? ??? ??? ?for(int i=0;i<x.length;i++){
?? ??? ??? ??? ?if(x[i]==2||x[i]==4||x[i]==5){
?? ??? ??? ??? ??? ?System.out.print(crent[x[i]].name+"\t");
?? ??? ??? ??? ??? ?oSum+=crent[x[i]].onum;
?? ??? ??? ??? ??? ?sumMon+=crent[x[i]].mon;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?for(int i=0;i<x.length;i++){
?? ??? ??? ??? ?if(x[i]==2){
?? ??? ??? ??? ??? ?sumMon=sumMon-crent[x[i]].mon;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?System.out.println("共載貨:"+oSum+"噸");
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?sumMon*=Day;
?? ??? ??? ?System.out.print("***租車總價格:"+sumMon);
?? ??? ??? ?
?? ??? ?}else{
?? ??? ??? ?System.out.println("退出系統(tǒng)");
?? ??? ?}
?? ?}
}
關(guān)于面向?qū)ο髢?nèi)容最后的一個習(xí)題。然后我這個代碼我覺得輸出的代碼寫的不好,有沒大神能幫忙改正一下,而且這個存在BUG,如果選的車輛序號是同一輛最后會輸出多個相同車名,如果要更改就需要增加一段代碼進(jìn)行判斷是否有重復(fù)車序,所以我希望能有更簡便的代碼有沒可以分享相互學(xué)習(xí)學(xué)習(xí),萬分感謝。
2016-11-15
package demo.faceobj;
public abstract class Car {
?? ?public String name;
?? ?public int mon;
?? ?public int pnum;
?? ?public int onum;
?? ?public Car(){
?? ??? ?this.name=null;
?? ??? ?this.mon=0;
?? ??? ?this.pnum=0;
?? ??? ?this.onum=0;
?? ?}
?? ?public abstract void print();
}
***************
package demo.faceobj;
public class peCar extends Car {
?? ?public peCar(String newname,int newmon,int newpnum){
?? ??? ?this.name=newname;
?? ??? ?this.mon=newmon;
?? ??? ?this.pnum=newpnum;
?? ?}
?? ?public void print(){
?? ??? ?System.out.println(name+"\t"+mon+"元/天"+"\t"+"載人:"+pnum+"人");
?? ?}
}
*************
package demo.faceobj;
public class obCar extends Car {
?? ?public obCar(String newname,int newmon,int newonum){
?? ??? ?this.name=newname;
?? ??? ?this.mon=newmon;
?? ??? ?this.onum=newonum;
?? ?}
?? ?public void print(){
?? ??? ?System.out.println(name+"\t"+mon+"元/天"+"\t"+"載貨:"+onum+"噸");
?? ?}
}
****************
package demo.faceobj;
public class pickCar extends Car {
?? ?public pickCar(String newname,int newmon,int newpnum,int newonum){
?? ??? ?this.name=newname;
?? ??? ?this.mon=newmon;
?? ??? ?this.pnum=newpnum;
?? ??? ?this.onum=newonum;
?? ?}
?? ?public void print(){
?? ??? ?System.out.println(name+"\t"+mon+"元/天"+"\t"+"載人:"+pnum+"? 載貨:"+onum+"噸");
?? ?}
}
2016-11-14
我不是大神哈,但是你能把其他幾個子class粘出來一下嗎?不然得自己編。