我的代码如下:
package com.exerciseTWO; import java.util.Scanner; public class Test { public void zuche() { Scanner input = new Scanner(System.in); int p = input.nextInt(); if(p==1){ zhuliucheng(); }else if(p == 2){ System.out.println("感谢您使用大大租车系统~"); }else{ System.out.println("您输入的有误,请重新输入"); System.out.println("您是否需要租车:1、是" + "\t" + "0、否"); zuche(); } } public void zhuliucheng(){ Vehicle cars [] = { new PassengerCar(1,"奥迪A4",500,4), new PassengerCar(2,"马自达6",400,4), new PickUpCar(3,"皮卡雪6",450,4,2), new PassengerCar(4,"金龙",800,20), new TrunkCar(5,"松花江",400,4), new TrunkCar(6,"依维柯",1000,20) }; //输出车辆的信息列表 System.out.println("您可组车的类型及其价目表:\n序号 汽车名称 租金 容量" ); for(int i=0;i<cars.length;i++) { cars[i].print(); } //输出租车数量 System.out.println("请输入你想要租车的数量"); Scanner carsNumInput= new Scanner(System.in); int carsNum= carsNumInput.nextInt();//输入租车数量 int carsNumArraylist[] = new int[carsNum]; int totalPassenger = 0;// 总人数 int totalTrunk=0;//总重量 double totalPrice=0;//总价格 if(carsNum>0){ for(int o=0;o<carsNum;o++){ System.out.println("请输入您需要的第"+(o+1)+"辆车的号码(序号)"); int id= carsNumInput.nextInt();//输入了每个序号 totalPassenger= totalPassenger +cars[id-1].PassengerNum; totalPrice = totalPrice + cars[id-1].CarPrice; totalTrunk = totalTrunk + cars[id-1].TrunkNum; carsNumArraylist[o] = id; } System.out.println("请输入你需要租用的天数"); int day = carsNumInput.nextInt(); System.out.println("*******可载人的车有:"); for(int i =0;i<carsNumArraylist.length;i++){ System.out.println(cars[carsNumArraylist[i]-1].CarName ); } System.out.println("总载人:"+ totalPassenger); System.out.println("*******可载货的车有:"); for(int i = 0;i<carsNumArraylist.length;i++) { System.out.println(cars[carsNumArraylist[i]-1].CarName); } System.out.println("总载重:"+totalTrunk); totalPrice =totalPrice*day; System.out.println("*******租车总价格:"+"\n" + totalPrice +"元"); System.out.println("进入正确流程"); }else{ System.out.println("输入编号有误直接退出系统"); } } public static void main(String[] args) { System.out.println("欢迎使用大大租车系统!"); System.out.println("请问您是否需要租车?"+"\t" + "1.是" +"\t"+"2.否"); Test demoMain = new Test(); demoMain.zuche(); } }
加条件判断