新建一个car类
package com.axhu;
public class Car {
String name;
int money;
int personnum;
int burden;
}
Burdencar类
package com.axhu;
public class Burdencar extends Car {
public Burdencar(String newname,int newmoney,int newburden){
this.name=newname;
this.money=newmoney;
this.burden=newburden;
}
}
Personcar类
package com.axhu;
public class Personcar extends Car {
public Personcar(String newname,int newmoney,int newpersonnum){
this.name=newname;
this.money=newmoney;
this.personnum=newpersonnum;
}
}
BurdenPersoncar类
package com.axhu;
public class BurdenPersoncar extends Car {
public BurdenPersoncar(String newname,int newmoney,int newpersonnum,int newburden){
this.name=newname;
this.money=newmoney;
this.personnum=newpersonnum;
this.burden=newburden;
}
}
main Test类
package com.axhu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Personcar c1 = new Personcar("奥迪A4",500,4);
Personcar c2 = new Personcar("马自达8",400,4);
BurdenPersoncar c3 = new BurdenPersoncar("皮卡雪8",450,4,2);
Personcar c4 = new Personcar("金龙",800,20);
Burdencar c5 = new Burdencar("松花江",400,4);
Burdencar c6 = new Burdencar("依维柯",1000,20);
System.out.println("欢迎使用哒哒租车系统");
System.out.println("你是否需要租车 1是 0否");
Scanner input = new Scanner(System.in);
int r = input.nextInt();
if(r==1){
System.out.println("你可能租车的类型以及价目表:");
System.out.println("序号 汽车名称 租金 容量");
System.out.println("1.\t"+c1.name+"\t"+c1.money+"元/天 "+"载人:"+c1.personnum+"人");
System.out.println("2.\t"+c2.name+"\t"+c2.money+"元/天 "+"载人:"+c2.personnum+"人");
System.out.println("3.\t"+c3.name+"\t"+c3.money+"元/天 "+"载人:"+c3.personnum+"人"+" 载货:"+c3.burden+"吨");
System.out.println("4.\t"+c4.name+"\t"+c4.money+"元/天 "+"载人:"+c4.personnum+"人");
System.out.println("5.\t"+c5.name+"\t"+c5.money+"元/天 "+"载货:"+c5.burden+"吨");
System.out.println("6.\t"+c6.name+"\t"+c6.money+"元/天 "+"载货:"+c6.burden+"吨");
System.out.println("请输入租车的数量");
int quality = input.nextInt();
int money=0;
int burden=0;
int personnum=0;
String n1 ="";
String n2 ="";
for(int i=1;i<=quality;i++){
System.out.println("请输入第"+i+"辆车的序号:");
int num =input.nextInt();
switch(num)
{
case 1:
n1+=c1.name;
money+=c1.money;
personnum+=c1.personnum;
break;
case 2:
n1+=c2.name;
money+=c2.money;
personnum+=c2.personnum;
break;
case 3:
n1+=c3.name;
n2+=c3.name;
money+=c3.money;
personnum+=c3.personnum;
burden+=c3.burden;
break;
case 4:
n1+=c4.name;
money+=c4.money;
personnum+=c4.personnum;
break;
case 5:
n2+=c5.name;
money+=c5.money;
burden+=c5.burden;
break;
case 6:
n2+=c6.name;
money+=c6.money;
burden+=c6.burden;
break;
}
}
System.out.println("请输入租车的天数:");
int days = input.nextInt();
System.out.println("你的账单:");
System.out.println("***你租载人的车有:***");
System.out.println(n1+"一共载人的人数:"+personnum+"人");
System.out.println("***你租载货的车有:***");
System.out.println( n2+"一共载货数:"+burden+"吨");
System.out.println("你的租车天数:"+days);
System.out.println("你租车的总费用为:"+days*money+"元");
}
else if(r==0){
System.out.println("谢谢你的支持,欢迎下次光临!");
}
else{
System.out.println("你输入的信息有误!请重新开始!");
}
}
}
结果截图
![图片描述][1]
热门评论
忽略了一个问题,比如说输入序号不在所给范围内,那么应该提示错误重新输入才行
太简单了点,如果初学JAVA还可以,如果学了挺久了~~~~你懂的
还可以