作業提交了,后面的賬單本來想做好一點的,可是想了好久最不出來,就投機取巧的這樣寫了,求大佬幫幫忙改一下啊
package package3;
import java.util.Scanner;
//抽象類
?abstract class car{
//public static int length;
protected String carName;//車名
protected int personNumber;//載客量
protected double goods;//載物量
protected int money;//租金
//客車構造方法,客車載客
public car(String carName,int personNumber,int money){
super();
this.carName=carName;
this.personNumber=personNumber;
this.money=money;
}
//貨車構造方法,貨車載人
public car(String carName,double goods,int money){
super();
this.carName=carName;
this.goods=goods;
this.money=money;
}
//皮卡構造方法,皮卡即載客又載人
public car(String carName,int personNumber,double goods,int money){
super();
this.carName=carName;
this.personNumber=personNumber;
this.goods=goods;
this.money=money;
}
abstract void show();//抽象類中必須有抽象方法,且方法中不能含有方法體
}
?//客車類
? ?class passengerCar extends car{
public passengerCar(String carName,int personNumber, int money) {
super(carName, personNumber, money);??
? }
public void show(){
System.out.println(carName+"? "+money+"元/天? 載人:"+personNumber+"人");
}
}
? //貨車類
? ?class lorry extends car{
public lorry(String carName, double goods, int money) {
super(carName, goods, money);
}
??
??
public void show(){
System.out.println(carName+"? "+money+"元/天? ?載貨:"+goods+"噸");
}
}
? //皮卡類
? ?class pika extends car{
public pika(String carName,int personNumber,double goods,int money) {
super(carName,personNumber,goods,money);
}
public void show(){
System.out.println(carName+"? "+money+"元/天? 載貨:"+goods+"噸? 載人"+personNumber );
}
}
public class Cartest {
public static void main(String[] args) {
car[] cars={new lorry ("松花江",4.0, 400),
? ? new lorry ("依維柯",20.0,1000),
? ? new pika ("皮卡雪", 2, 2, 450),
? ? new passengerCar("馬自達", 4, 400),
? ? new passengerCar("奧迪A4", 4, 500)};
? ? ? ? ? ? ? ??
System.out.println("歡迎使用噠噠租車系統:");
System.out.println("你是否要租車:1-是? ? 0-否");
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
if(i==1){
System.out.println("你可租車的類型及其價目錄:");
System.out.println("********************************");
for(int a=0;a<cars.length;a++){
System.out.print((a+1) +"? " );
cars[a].show();
}
System.out.println("請輸入你要租的汽車數量:");
int number=sc.nextInt();
int Money=0;
for(int j=0;j<number;j++){
System.out.printf("請輸入第%d輛車的序號:",j+1);
int a=sc.nextInt();
System.out.println("你選中的第"+(j+1)+"車為:——————"+cars[a-1].carName+"? ? 這輛車每天的租金為:"+cars[a-1].money);
Money=Money+cars[a-1].money;
}
System.out.println("請輸入租車天數:");
int days=sc.nextInt();
System.out.println("***********租車完成***********");
System.out.println("下面開始統計賬單:");
System.out.println("您一共租了"+number+"輛車,一共花費"+(Money*number)+"元");
}
else{
System.out.println("謝謝!歡迎下次再來");
System.exit(0);
}
}
}
2019-05-04
參考大佬代碼,在下還是沒能獨立設計出程序
2018-11-08
用不用抽象類影響不大,反正后面學了接口之后就很少用到這個了
2018-11-07
我也是初學者,不過我并沒有建立一個車的數組,而是使用switch函數來獲取信息,覺得你的代碼更簡潔一些。