不怎么满意,但在参考一位同学的代码后总算是写出来了
主程序:
package com.zuche;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Aodi vehilcle1=new Aodi(); //父类无法调用子类属性
Mazida vehilcle2=new Mazida(); //若使用Vehicle vehilcle1=new Aodi();
Pika vehilcle3=new Pika(); //下面步骤无法调用子类属性的price
Jinlong vehilcle4=new Jinlong();
Songhuajiang vehilcle5=new Songhuajiang();
Yiweike vehilcle6=new Yiweike();
int sumP=0;
int sumCP=0;
int sumCG=0;
int n1=0;
int n2=0;
int n3=0;
do{
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
if (i==0){
System.out.println("谢谢使用!");
}else if(i!=1){
System.out.println("输入错误,请重新输入");
}else{
break;
}
// sc.close();
}while(true);
System.out.println("您可租车的类型和价目表:");
System.out.println("序号\t汽车名称\t租金\t\t载客量\t载货量");
System.out.println("1."+"\t"+vehilcle1.name+"\t"+vehilcle1.price+"元/天"+"\t"+"\t"+vehilcle1.carryPeople+"人"+"\t"+vehilcle1.carryGoods+"吨");
System.out.println("2."+"\t"+vehilcle2.name+"\t"+vehilcle2.price+"元/天"+"\t"+"\t"+vehilcle2.carryPeople+"人"+"\t"+vehilcle2.carryGoods+"吨");
System.out.println("3."+"\t"+vehilcle3.name+"\t"+vehilcle3.price+"元/天"+"\t"+"\t"+vehilcle3.carryPeople+"人"+"\t"+vehilcle3.carryGoods+"吨");
System.out.println("4."+"\t"+vehilcle4.name+"\t"+vehilcle4.price+"元/天"+"\t"+"\t"+vehilcle4.carryPeople+"人"+"\t"+vehilcle4.carryGoods+"吨");
System.out.println("5."+"\t"+vehilcle5.name+"\t"+vehilcle5.price+"元/天"+"\t"+"\t"+vehilcle5.carryPeople+"人"+"\t"+vehilcle5.carryGoods+"吨");
System.out.println("6."+"\t"+vehilcle6.name+"\t"+vehilcle6.price+"元/天"+"\t"+vehilcle6.carryPeople+"人"+"\t"+vehilcle6.carryGoods+"吨");
System.out.println("请输入您要租车的数量:");
Scanner sc2=new Scanner(System.in);
int i2=sc2.nextInt();
for(int j=1;i2>0;i2--,j++){
System.out.println("请输入第"+j+"辆车的序号:");
Scanner sc3=new Scanner(System.in);
int i3=sc3.nextInt();
if (i3>6){
System.out.println("输入错误,请重新输入:");
Scanner sc4=new Scanner(System.in);
i3=sc4.nextInt();
}
switch(i3){
case 1:
vehilcle1.introduce();
n1=vehilcle1.price;
n2=vehilcle1.carryPeople;
n3=vehilcle1.carryGoods;
break;
case 2:
vehilcle2.introduce();
n1=vehilcle2.price;
n2=vehilcle2.carryPeople;
n3=vehilcle2.carryGoods;
break;
case 3:
vehilcle3.introduce();
n1=vehilcle3.price;
n2=vehilcle3.carryPeople;
n3=vehilcle3.carryGoods;
break;
case 4:
vehilcle4.introduce();
n1=vehilcle4.price;
n2=vehilcle4.carryPeople;
n3=vehilcle4.carryGoods;
break;
case 5:
vehilcle5.introduce();
n1=vehilcle5.price;
n2=vehilcle5.carryPeople;
n3=vehilcle5.carryGoods;
break;
case 6:
vehilcle6.introduce();
n1=vehilcle6.price;
n2=vehilcle6.carryPeople;
n3=vehilcle6.carryGoods;
break;
}
System.out.println("借几天?");
Scanner sc5=new Scanner(System.in);
int i5=sc5.nextInt();
System.out.println("借"+i5+"天");
sumP=sumP+n1*i5;
sumCP=sumCP+n2;
sumCG=sumCG+n3;
}
System.out.println("共计金额:"+sumP+"元");
System.out.println("载客量:"+sumCP+"人");
System.out.println("载货量:"+sumCG+"吨");
}
}
车辆父类:
package com.zuche;
public abstract class Vehicle {
String name;
public int price;
public int carryPeople;
public int carryGoods;
public abstract void introduce();
}
各种车辆子类:
package com.zuche;
public class Aodi extends Vehicle {
public String name="奥迪A4";
public int price=500;
public int carryPeople=4;
@Override
public void introduce() {
// TODO Auto-generated method stub
System.out.println(name);
}
}
package com.zuche;
public class Mazida extends Vehicle {
public String name="马自达6";
public int price=400;
public int carryPeople=4;
@Override
public void introduce() {
// TODO Auto-generated method stub
System.out.println(name);
}
}
package com.zuche;
public class Pika extends Vehicle {
public String name="皮卡雪6";
public int price=450;
public int carryPeople=4;
public int carryGoods=2;
@Override
public void introduce() {
// TODO Auto-generated method stub
System.out.println(name);
}
}
package com.zuche;
public class Jinlong extends Vehicle {
public String name="金龙";
public int price=800;
public int carryPeople=20;
@Override
public void introduce() {
// TODO Auto-generated method stub
System.out.println(name);
}
}
package com.zuche;
public class Songhuajiang extends Vehicle {
public String name="松花江";
public int price=800;
public int carryGoods=4;
@Override
public void introduce() {
// TODO Auto-generated method stub
System.out.println(name);
}
}
package com.zuche;
public class Yiweike extends Vehicle {
public String name="依维柯";
public int price=1000;
public int carryGoods=20;
@Override
public void introduce() {
// TODO Auto-generated method stub
System.out.println(name);
}
}
热门评论
这个main函数程序有BUG,如果输入i3>6,虽然显示有错,但是接着输入一个大于6的数,会跳转到租车天数,并且结算时也是按照最后一辆车的价格结算