为了账号安全,请及时绑定邮箱和手机立即绑定

写了一下午哎。。。

Car类

package com.imooc;


public  class Car {

public String name;

public int population;

public int price;

public int load;

public int number;

public void function_print(int num,String name,int price,int load,int population) {

if(load==0)

System.out.println(num+".\t"+name+"\t\t"+price+"元/天"+"\t\t载人:"+population+"人");

if(population==0)

System.out.println(num+".\t"+name+"\t\t"+price+"元/天"+"\t\t载货:"+load+"吨");

if(load!=0&&population!=0)

System.out.println(num+".\t"+name+"\t\t"+price+"元/天"+"\t\t载人:"+population+"人  "+"载货:"+load+"吨");


}

}




Bus类

package com.imooc;


public class Bus extends Car{

public int load=0;


}






Truck类

package com.imooc;


public class Truck extends Car {


public int population=0;



}






pickup类

package com.imooc;


public class Pickup extends Car {




}


Inital类

package com.imooc;



import java.util.*;

import java.awt.AWTException;

import java.awt.Robot;

public class Initial {

public static void main(String[] args) throws AWTException {

//系统初始化

int num=6;

int days=0;

int total_people=0;

int total_load=0;

int total_price=0;


int[] rent_number=new int[6];

Robot  r   = new Robot(); 

Car[] car=new Car[6];

car[0]=new Bus();

car[0].name="奥迪A4";

car[0].price=500;

car[0].population=4;

car[1]=new Bus();

car[1].name="马自达6";

car[1].price=400;

car[1].population=4;

car[2]=new Bus();

car[2].name="金龙";

car[2].price=800;

car[2].population=20;

car[3]=new Truck();

car[3].name="松花江";

car[3].price=400;

car[3].load=4;

car[4]=new Truck();

car[4].name="依维柯";

car[4].price=900;

car[4].load=20;

car[5]=new Pickup();

car[5].name="皮卡雪6";

car[5].price=450;

car[5].population=4;

car[5].load=2;

//系统初始化结束,开始运行

System.out.println("Welcome to the dada rent_car system\n");

System.out.println("If you want to rent car?\n1.yes\n2.no");

Scanner flag1=new Scanner(System.in);

int flag2=flag1.nextInt();

//输入判断

while(flag2!=1&&flag2!=2)

{

System.out.println("选择错误,请重新选择");

flag2=flag1.nextInt();

}

if(flag2==1)

{

}

else{

System.out.println("已选择,程序将于2秒后关闭");

r.delay(2000); 

System.exit(0);

}

//打印可租的车

System.out.println("\n\n以下为可以租用的车\n");

System.out.println("序号\t汽车名称\t\t租金\t\t容量\t");

for(int i=0;i<6;i++) {

car[i].function_print(i+1, car[i].name,car[i].price,car[i].load,car[i].population);


}

//选车

System.out.println("\n请输入租车数量");

Scanner amount=new Scanner(System.in);

int amount1=amount.nextInt();

while(amount1<0||amount1>6)

{

System.out.println("目前系统仅有"+num+"辆车,请重新输入");

amount1=amount.nextInt();

}

if(amount1==0) {

System.out.println("不租车,系统将于2秒后关闭");

r.delay(2000); 

System.exit(0);

}

else {

for(int i=0;i<amount1;i++) {

System.out.println("请输入第"+(i+1)+"辆车的序号(从1到6):");

rent_number[i]=new Scanner(System.in).nextInt();

while(rent_number[i]<1||rent_number[i]>6)

{

System.out.println("输入错误,请重新输入");

rent_number[i]=new Scanner(System.in).nextInt();

}

}

}

//选车完毕,结算

System.out.println("\n\n请输入租车天数:");

days=amount.nextInt();

System.out.println(days);

System.out.println("\n\n您的账单如下,请确认");

System.out.println("********************************************************************");

for(int i=0;i<amount1;i++) {

car[(rent_number[i]-1)].function_print((rent_number[i]-1)+1, car[(rent_number[i]-1)].name,car[(rent_number[i]-1)].price,car[(rent_number[i]-1)].load,car[(rent_number[i]-1)].population);

total_people+=car[(rent_number[i]-1)].population;

total_load+=car[(rent_number[i]-1)].load;

total_price+=car[(rent_number[i]-1)].price;

}

System.out.println("********************************************************************");

System.out.println("\n总载人数为:"+total_people+"人");

System.out.println("总载货量为:"+total_load+"吨");

System.out.println("租期为:\t"+days+"天");

System.out.println("总费用为:"+total_price+"元");




}


}

https://img1.sycdn.imooc.com//5c5bf1760001d76708990780.jpg

https://img1.sycdn.imooc.com//5c5bf1620001dc4506830304.jpg



正在回答

2 回答

算总费用的时候好像有问题,没乘以租车天数

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

写了一下午哎。。。

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信