手记

JAVA入门第二季最终练习

/定义一个父元素/
package com.imooc;
//定义一个父类元素
public class Car {
public String Carname;//定义车辆名称
public float Price;//定义车辆价格
public int PeopleNumber;//容纳人的数量
public float Height;//货物数量

}

/定义三个子类元素/
package com.imooc;

public class keche extends Car {
public keche(String Carname,float Price,int PeopleNumber ){
this.Carname = Carname;
this.Price = Price;
this.PeopleNumber = PeopleNumber;
}
//定义一个输出方法
public String toString(){
return ("\t"+Carname+"\t" + Price+"\t\t" + PeopleNumber+"人");
}

}
package com.imooc;

public class huoche extends Car {
public huoche(String Carname,float Price,float Height){

    this.Carname = Carname;
    this.Price = Price;
    this.Height = Height;
}
//定义一个输出方法
public String toString(){
    return ("\t"+Carname +"\t"+ Price +"\t\t"+ Height+"吨");
}

}
package com.imooc;

public class pika extends Car {
public pika(String Carname,float Price,int PeopleNumber,float Height){
this.Carname = Carname;
this.Price = Price;
this.PeopleNumber = PeopleNumber;
this.Height = Height;
}
//定义一个输出方法
public String toString(){
return ("\t"+Carname +"\t"+ Price+"\t\t" + PeopleNumber+"人" + Height+"吨");
}

}

/主函数/
package com.imooc;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Car car[] =new Car[]{
            new keche("奥迪A4",500,4),
            new keche("马自达6",400,4), 
            new keche("金龙",800,20), 
            new huoche("松花江",400,4), 
            new huoche("依维柯",1000,20), 
            new pika("皮卡雪",450,4,2) 
            };
    System.out.println("/*********欢迎来到答答租车系统**********/");
    System.out.println("/*********请问是否需要租车***********/");
    System.out.println("/***********是(1)  否(2)*********/");
    Scanner in1 = new Scanner(System.in);

    if( in1.nextInt() == 1){
        System.out.println("欢迎选择答答租车,请选择车型");
        System.out.println("序号\t车型\t价格(元/天)\t容纳量");
        for(int i=0;i<car.length;i++){
            System.out.println((i+1)+"、"+car[i]);
        }
        System.out.println("请输入你要租车的数量");
        float zongjia = 0;

        int a =  in1.nextInt();
        for(int i=1;i<a+1;i++){

            System.out.println("请输入你要租的车的第"+i+"辆车的序号:"); 
            int b =  in1.nextInt();
            System.out.println(car[b-1]);
            System.out.println("请输入你要租用的天数:");
            int c = in1.nextInt();
            zongjia += car[b-1].Price * c;

        }
        System.out.println("您租车的总价为" + zongjia);
        System.out.println("请问您是否现在支付费用?       是(1)   否(2)");
        int d = in1.nextInt();
        if(d == 1){
            System.out.println("请输入您要支付的费用:");
            int e = in1.nextInt();
            if(e == zongjia ){
                System.out.println("支付成功,请提取您的车辆!祝您旅途愉快");
            }
            else{
                System.out.println("对不起,您支付的金额有误,请重新输入。");
            }
        }
        else{
            System.out.println("欢迎您下次使用");
            }
    }
    else{
        System.out.println("欢迎您下次使用");
    }

}

}
/部分代码参考了其他慕友的写法/

1人推荐
随时随地看视频
慕课网APP