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

JAVA入门第二季 6-1综合练习

标签:
Java
  • 父类
    public class Car {
    //int people;
    String name;
    int money;
    public Car(String name1,int money1){
    name=name1;
    money=money1;
    }
    }
  • 子类1(只载人)
    public class PassengerCar extends Car {
    int people;
    public PassengerCar(String name1, int money1,int people1) {
    super(name1, money1);
    people=people1;
    System.out.println("1\t"+name+"\t"+money+"\t"+"载人"+people+"人\t" );
    }
    }
  • 子类2(只载物)
    public class Van extends Car {
    // String name;
    int weight;
    public Van(String name1,int money1,int weight1){
    super(name1, money1);
    name=name1;
    money=money1;
    weight=weight1;
    System.out.println("6\t"+name+"\t"+money+"\t"+"载货"+weight+"吨\t" );
    }
    }
  • 子类3(载人又载物)
    public class Truck extends Car {
    int people;
    int weight;
    public Truck(String name1, int money1,int people1,int weight1) {
    super(name1, money1);
    people=people1;
    weight=weight1;
    System.out.println("3\t"+name+"\t"+money+"\t"+"载人"+people+"人"+"载货"+weight+"吨\t" );
    }
    }
  • 测试类
    import java.util.Scanner;

public class Test {

public static void main(String[] args) {
int sum=0;//金额
String n = "";//记录载人的车子
String w="";//记录载物的车子
int p=0;//计算载人总人数
int h=0;//货物总量
System.out.println("是否选择哒哒租车系统:1是 0否");
Scanner input=new Scanner(System.in);
// int a=input.nextInt();
if(input.nextInt()==1){
System.out.println("序号\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量\t ");
PassengerCar aCar=new PassengerCar("奥迪A4",500,4);
PassengerCar bCar=new PassengerCar("马自达6",500,4);
Truck cCar=new Truck("皮卡雪6",450,2,2);
PassengerCar dCar=new PassengerCar("金龙",800,20);
Van eCar=new Van("松花江",400, 4);
Van fCar=new Van("依维柯",1000,20);
System.out.println("请输入你需要租车的数量");
Scanner intput=new Scanner(System.in);
int b = input.nextInt();//接受你租车的数量
for(int i=1;i<=b;i++){
System.out.println("请输入第"+i+"辆车的序号: ");
int no=input.nextInt();
switch (no) {
case 1:
sum=sum+aCar.money;
n=n+aCar.name+" ";
p=p+aCar.people;
break;
case 2:
sum=sum+bCar.money;
n=n+bCar.name+" ";
p=p+bCar.people;
break;
case 3:
sum=sum+cCar.money;
n=n+cCar.name+" ";
w=w+cCar.name+" ";
p=p+cCar.people;
h=h+cCar.weight;
break;
case 4:
sum=sum+dCar.money;
n=n+dCar.name+" ";
p=p+dCar.people;
break;
case 5:
sum=sum+eCar.money;
w=w+eCar.name+" ";
h=h+eCar.weight;
break;
case 6:
sum=sum+fCar.money;
w=w+fCar.name+" ";
h=h+fCar.weight;
break;
}
}
System.out.println("请输入租车天数: ");
int c=input.nextInt();//接收租车天数
System.out.println("载人的车子有:"+n+" 能载人:"+p+"人");
System.out.println("载物的车子有:"+w+" 能载物:"+h+"吨");
System.out.println("你的租金"+sum*c);
}
else {
System.err.println("你已经退出本系统");
}
}
}

  • 结果图片描述
点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消