package com.imooc; import java.util.Scanner; public class Hello { public static void main(String[] args) { // TODO Auto-generated method stub String[] carName={"奥迪A4","马自达6","皮卡雪6","金龙","松花江","依维柯 "};//车名 int[] rentMoney={ 500,400,450,800,400,1000};//租金 int[] capacity={ 4, 4, 4,20,0,0};//载客 int[] carryCargo={0,0,2,0, 4,20};//载重 System.out.println("欢迎使用答答租车系统"); System.out.println("您是否要租车:1是 0否"); Scanner input = new Scanner(System.in); while (true) { int input1=input.nextInt(); if(input1 == 0 || input1 == 1){ if(input1 == 0){ System.out.println("谢谢使用,再见!"); }else{ System.out.println("您可租车的类型及其价目表"); System.out.println("序号 汽车名称 租金 容量");//空格只为对齐格式 System.out.println("1. " +carName[0]+" "+rentMoney[0]+"元/天"+" 载人:"+capacity[0]+"人"); System.out.println("2. " +carName[1]+" "+rentMoney[1]+"元/天"+" 载人:"+capacity[1]+"人"); System.out.println("3. " +carName[2]+" "+rentMoney[2]+"元/天"+" 载人:"+capacity[2]+"人 "+"载货:"+carryCargo[2]+"吨"); System.out.println("4. " +carName[3]+" "+rentMoney[3]+"元/天"+" 载人:"+capacity[3]+"人"); System.out.println("5. " +carName[4]+" "+rentMoney[4]+"元/天"+" 载货:"+carryCargo[4]+"吨"); System.out.println("6. " +carName[5]+" "+rentMoney[5]+"元/天"+" 载货:"+carryCargo[5]+"吨"); break; } }else{ System.out.println("输入有误请重新输入!"); } } double sumMon = 0;//租车总金额 int sumCap = 0;//租车总载客 double sumCar = 0;//租车总载重 String capList ="";//载客汽车清单 String carList ="";//载货汽车清单 System.out.println("请输入您要租汽车的数量:"); while (true) { int num=input.nextInt(); if(num<=0||num>6){ System.out.println("请输入1-6范围内的数字!"); }else{ break; } break; } int num=input.nextInt(); for(int i=1;i<=6&&i<=num;i++){ System.out.println("请输入第"+i+"辆车的序号:"); int carNum=input.nextInt(); sumMon = sumMon+rentMoney[carNum - 1]; sumCap = sumCap+capacity[carNum - 1]; sumCar = sumCar+carryCargo[carNum - 1]; int m=carNum; switch (m){ case 1: case 2: case 4: capList = capList+" "+carName[carNum - 1]; break; case 5: case 6: carList = carList+" "+carName[carNum - 1]; break; case 3: capList = capList+" "+carName[carNum - 1]; carList = carList+" "+carName[carNum - 1]; break; } } System.out.println("请输入租车天数:"); int days=input.nextInt(); System.out.println("您的账单:"); System.out.println("***载人的车有:"); System.out.print(capList+" "); System.out.println("共载人:"+sumCap+"人"); System.out.println("***载货的车有:"); System.out.print(carList+" "); System.out.println("共载货:"+sumCar+"吨"); System.out.println("***租车总价格:"+days*sumMon+"元"); input.close(); } }
刚开始学的纯新,试着实现了一下视频里最终的效果..
本来也有父类和子类,但试了很久也没办法while调用出子类中的数据,最后没办法就直接在主代码里用数组把6辆车的变量都定义出来了
想问的就是有什么便利的办法让客户输入异常数据时可以提示异常并循环方法
我的处理方法:
int isLeaseCar; // 是否要租车。
do{
sysout"是否要租车?:1-是,0-否"
isLeaseCar = input.nextInt();
if(isLeaseCar==1){ // 是
}else if(isLeaseCar==0){ // 否
}else{ // 处理输入异常
sysout"请选择1或0"
}
while(isLeaseCar !=0&&isLeaseCar!=1); // 输入的不是1或0的话就会一直循环。
}
写的很好,等晚上我帮你看看