继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

答答租车作业,没用到继承,多态.但是很简洁,很短.供大家共同学习.

慕粉2102399897
关注TA
已关注
手记 1
粉丝 0
获赞 1

package com.imooc;
//创建一个Car类
public class Car {
String name ;
int pay ;
int id;
int huo;
int ke;
public Car(int id,String name,int pay,int huo,int ke){
this.id = id;
this.name =name;
this.pay = pay;
this.huo = huo;
this.ke = ke;
}
}
主类
package com.imooc;

import java.util.Scanner;

public class Dada {

public static void main(String[] args) {
    System.out.println("欢迎来到答答租车系统\n"+
                    "您是否要租车:1是0否");
    Scanner sc = new Scanner(System.in);
    int i = sc.nextInt();
    if (i <1) {

    }
    System.out.println("您可租的车型如下:");
    System.out.println("ID\t名称\t租金\t载货量\t载客量");
    Car [] che = {new Car(1,"奥迪A4",500,0,4),
            new Car(2,"马自达6",400,0,4),
            new Car(3,"皮卡雪6",450,2,4),
            new Car(4,"金龙",800,0,20),
            new Car(5,"松花江",400,4,0),
            new Car(6,"依维柯",1000,20,0)};
    for (int j = 0; j < che.length; j++) {
        System.out.println(che[j].id +"\t" + che[j].name +"\t"+ che[j].pay +"元/天"+"\t"+
                            che[j].huo + "\t" +che[j].ke);
    }
    System.out.println("请输入您要租车的数量:");
    int num = sc.nextInt();
    Car [] zu =new Car[num];
    for (int j = 0; j < zu.length; j++) {
        System.out.println("请输入你选择的车辆ID");
        zu[j] = che[sc.nextInt()-1];
    }
    System.out.println("请输入你租用天数");
    int tian = sc.nextInt();
    int pays = 0 ;
    int keliang = 0;
    int huoliang = 0;
    System.out.println("您租用的车辆信息如下:");
    for (int j = 0; j < zu.length; j++) {
    System.out.println((j+1) +"\t" + zu[j].name +"\t"+ zu[j].pay +"元/天");
    pays += zu[j].pay;
    keliang += zu[j].ke;
    huoliang += zu[j].huo;
    }
    System.out.println("一共租用"+tian+"天,"+"可载货"+huoliang+"T,"+
            "可载人"+keliang+"人,"+"租金一共"+ pays*tian +"元.");

}

}

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP