看完java教程后_这是看完Java入门第二季教程的综合练习

代码相对来说比较简单,希望有人看到并能提供代码的精简和优化

在这里输入代码

package com.car;

public class Car {

public int carid;

public Car(int carid){

this.carid = carid;

}

}

package com.car;

public class Xiaojiaoche extends Car {

//private int carid;

private String carname;

private int usermoney;

private int usercapacity;

public Xiaojiaoche(int carid,String carname,int usermoney,int usercapacity) {

// TODO 自动生成的构造函数存根

super(carid);

this.carid = carid;

this.carname = carname;

this.usermoney = usermoney;

this.usercapacity = usercapacity;

}

public int getCarid() {

return this.carid;

}

public String getCarname() {

return this.carname;

}

public int getUsermoney() {

return this.usermoney;

}

public int getUsercapacity() {

return this.usercapacity;

}

//方法重写,直接使用就行

public String toString() {

return carid+" "+carname+" "+usermoney+"元/天 载人:"+usercapacity+"人 "; }

}

package com.car;

public class Pika extends Car{

// public int carid;

public String carname;

public int usermoney;

public int usercapacity;

public int trainload;

public Pika(int carid,String carname,int usermoney,int usercapacity,int trainload) {

//this.carid = carid;

super(carid);

this.carname = carname;

this.usermoney = usermoney;

this.usercapacity = usercapacity;

this.trainload = trainload;

}

public int getCarid() {

return this.carid;

}

public String getCarname() {

return this.carname;

}

public int getUsermoney() {

return this.usermoney;

}

public int getUsercapacity() {

return this.usercapacity;

}

public int getTrainload() {

return this.trainload;

}

//方法重写,直接使用就行

public String toString() {

return carid+" "+carname+" "+usermoney+"元/天 载人:"+usercapacity+"人 载货:"+trainload+"吨 "

; }

}

package com.car;

public class Freightcar extends Car {

//定义成员

//private int carid;

private String carname;

private int usermoney;

private int trainload;

public Freightcar(int carid,String carname,int usermoney,int trainload) {

super(carid);

this.carname = carname;

this.usermoney = usermoney;

this.trainload = trainload;

}

public int getCarid() {

return this.carid;

}

public String getCarname() {

return this.carname;

}

public int getUsermoney() {

return this.usermoney;

}

public int getTrainload() {

return this.trainload;

}

//方法重写,直接使用就行

public String toString() {

return carid+" "+carname+" "+usermoney+"元/天 载货:"+trainload+"吨 "

; }

}

```package com.car;

import java.util.Scanner;

public class Text {

public static void main(String[] args) {

// TODO 自动生成的方法存根

int x=0; //用于存放选车的序列号

int flag; //判断是否要租车

String namecar1 = ""; //存放载人车的车名

String namecar2 = ""; //存放载货车的车名

int usercapacityvalue = 0; //存放总载人数

int trainloadvalue = 0; //存放总载货数

int usermoneyvalue = 0; //存放租车总费用

Scanner scn = new Scanner(System.in); //方便后面直接使用

Xiaojiaoche []c = new Xiaojiaoche[4];

Freightcar []f = new Freightcar[4];

c[0] = new Xiaojiaoche(1,"奥迪A4",500,4);

c[1] = new Xiaojiaoche(2,"马自达6",400,4);

c[2] = new Xiaojiaoche(4,"金龙",800,20);

Pika p = new Pika(3,"皮卡雪6",450,4,2);

f[0] = new Freightcar(5,"松花江",400,4);

f[1] = new Freightcar(6,"依维柯",1000,20);

System.out.println("欢迎使用答答租车系统:");

System.out.println("您是否要租车:1是 0否");

flag = scn.nextInt();

if(flag==1) {

//条件成立,无限循环

for(;;) {

System.out.println("您可租车的类型及其价位表:");

System.out.println("序号 汽车名称 租金 容量");

System.out.println(c[0]);

System.out.println(c[1]);

System.out.println(p);

System.out.println(c[2]);

System.out.println(f[0]);

System.out.println(f[1]);

System.out.println("请输入您想要租汽车的数量");

int i = scn.nextInt(); //获取控制台读取到的值

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

System.out.println("请输入第"+a+"辆车的序号:");

x = scn.nextInt();

//对读取到的序列号进行判断

if(x>0 && x< 5) {

switch(x) {

case 1: {

namecar1 = namecar1 + c[0].getCarname().toString()+" ";

usercapacityvalue +=c[0].getUsercapacity();

usermoneyvalue += c[0].getUsermoney();

break;

}

case 2: {

namecar1 = namecar1 + c[1].getCarname().toString()+" ";

usercapacityvalue +=c[1].getUsercapacity();

usermoneyvalue += c[1].getUsermoney();

break;

}

case 3: {

namecar1 = namecar1 + p.getCarname().toString()+" ";

usercapacityvalue += p.getUsercapacity();

usermoneyvalue += p.getUsermoney();

break;

}

case 4: {

namecar1 = namecar1 + c[2].getCarname().toString()+" ";

usercapacityvalue +=c[2].getUsercapacity();

usermoneyvalue += c[2].getUsermoney();

break;

}

}

}

if(x==3||x>4){

switch(x){

case 3: {

namecar2 = namecar2 + p.getCarname().toString()+" ";

trainloadvalue +=p.getTrainload();

break;

}

case 5: {

namecar2 = namecar2 + f[0].getCarname().toString()+" ";

trainloadvalue += f[0].getTrainload();

usermoneyvalue += f[0].getUsermoney();

break;

}

case 6: {

namecar2 = namecar2 + f[1].getCarname().toString()+" ";

trainloadvalue += f[1].getTrainload();

usermoneyvalue += f[1].getUsermoney();

break;

}

}

}

}

System.out.println("请输入租车天数");

usermoneyvalue *= scn.nextInt();

System.out.println("您的账单为:");

System.out.println("**************可载人的车有:");

System.out.println(namecar1+" "+"共载人:"+usercapacityvalue+"人");

System.out.println("**************可载货的车有:");

System.out.println(namecar2+" "+"共载货:"+trainloadvalue+"吨");

System.out.println("**************租车总价格:"+usermoneyvalue+"元");

// 防止数据累加

namecar1 = "";

namecar2 = "";

usercapacityvalue = 0;

trainloadvalue = 0;

usermoneyvalue = 0;

System.out.println("是否还想租车:1->继续 其他->退出");

if(scn.nextInt()!=1) {

System.exit(0); //关掉程序

}

}

}

else {

System.out.println("感谢您的使用");

System.exit(0);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值