慕粉0578318
2019-05-24 18:13
是我学习方法有问题吗,为什么思路关键是。写了一半发现错漏百出
可以给你个参考,还是很粗略的,不够严谨:
public class Begin {
public static void main(String[] args) {
System.out.println("欢迎使用滴答租车系统~~~");
System.out.println("是否开始订单?请按提示输入");
System.out.println("是:1 否:2");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if(a==2){
System.out.println("感谢使用~再见!");
return;
}
if(a==1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 名称 租金 容量");
Aodi ao = new Aodi(); //构造函数中就输出当前奥迪车的基本信息
Mazida ma = new Mazida();
Pikaxue pi = new Pikaxue();
JinLong jin = new JinLong();
SongHuaJiang song = new SongHuaJiang();
YiViKe yvk = new YiViKe();
}
System.out.println("您打算租几辆车?");
int b = sc.nextInt();
int money = 0, people = 0 , thing = 0; //定义总钱数,载人总数,载货总数;
int i = 1;
while(b != 0){
System.out.println("请选择第 " + i++ +"辆车:");
b--;
int c = sc.nextInt();
switch (c) {
case 1:
money += 500; //这里呢最好是调用Aodi.getRent();方法获取参数,这样就可以直接在奥迪类里改参数;
people += 4;
thing += 0;
break;
case 2:
money += 400;
people += 4;
thing += 0;
break;
case 3:
money += 450;
people += 4;
thing += 2;
break;
case 4:
money += 800;
people += 20;
thing += 0;
break;
case 5:
money += 400;
people += 0;
thing += 4;
break;
case 6:
money += 1000;
people += 0;
thing += 20;
break;
}
}
System.out.println("所以您最终订单总共需要缴纳人民币:" + money + "元");
System.out.println("共能载 " + people + " 人,载货 " + thing + " 吨");
}
}
下面展示奥迪类,其他类同,不过如果别的类(如金龙等)有“载货”这个属性,我就会加了一个capacity2属性,表示载货的量;
public class Aodi {
private int id = 1;
private final String name = "奥迪A4";
private final int rent = 500;
private final int capacity = 4; //限载4人
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public int getRent() {
return rent;
}
public int getCapacity() {
return capacity;
}
public Aodi() {
System.out.println(" " + id + " " + name + " " + rent
+ "元/天 载人:" + capacity + "人 ");
}
}
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题