为了账号安全,请及时绑定邮箱和手机立即绑定

自己写了一个,貌似输入范围外数值后会引起流程混乱,请高手看下谢谢!

package com.imooc.main;

import java.util.Scanner;

public class CarSystem {

	public static Scanner scanner;

	// 程序入口
	public static void main(String[] args) {
		isNeed();
	}

	// 判断是否需要进入系统
	public static void isNeed() {
		System.out.println("欢迎使用达达租车系统:");
		System.out.println("您是否要租车:1是  0否");
		scanner = new Scanner(System.in);
		String input = scanner.next();
		if (input.equals("1")) {
			list();
		} else if (input.equals("0")) {
			System.out.println("退出系统,欢迎下次再来!");
		} else {
			System.out.println("输入错误!请输入1或0");
			isNeed();
		}
	}

	// 生成车辆列表
	public static void list() {
		System.out.println("您可租车的类型及其价目表:");
		System.out.println("序号\t汽车名称\t租金\t容量");
		Vehicle audi = new Car(1, "奥迪A4", 500, 4);
		audi.say();
		Vehicle mazda = new Car(2, "马自达6", 400, 4);
		mazda.say();
		Vehicle pickup6 = new Pickup(3, "皮卡雪6", 450, 4, 2);
		pickup6.say();
		Vehicle jinlong = new Car(4, "金龙", 800, 20);
		jinlong.say();
		Vehicle songhuajiang = new Truck(5, "松花江", 400, 4);
		songhuajiang.say();
		Vehicle yiweike = new Truck(6, "依维柯", 1000, 20);
		yiweike.say();
		Vehicle[] carlist = { audi, mazda, pickup6, jinlong, songhuajiang,
				yiweike };
		rent(carlist);
	}

	// 选择车辆,同时计算租金和载人/载货数量
	public static void rent(Vehicle[] carlist) {
		int fee = 0; // 总金额
		int totalcontain_p = 0; // 总载人数
		double totalcontain_g = 0; // 总载货量
		System.out.println("请输入您要租车的数量:");
		int num = scanner.nextInt();
		int[] choose = new int[num];
		if (num > 0 && num <= carlist.length) {
			for (int i = 1; i <= num; i++) {
				System.out.println("请输入第" + i + "辆车的序号:");
				int inputserial = scanner.nextInt();
				if (inputserial > 0 && inputserial <= carlist.length) {
					choose[i - 1] = inputserial;
					fee += carlist[inputserial - 1].getPrice();
					totalcontain_p += carlist[inputserial - 1].getContain_p();
					totalcontain_g += carlist[inputserial - 1].getContain_g();
				} else {
					System.out.println("输入错误,请输入1到" + carlist.length + "的数字!");
					rent(carlist);
				}
			}
		} else {
			System.out.println("输入错误,请输入1到" + carlist.length + "的数字!");
			rent(carlist);
		}

		// 计算总金额
		System.out.println("请输入租车天数:");
		int day = scanner.nextInt();
		if (day <= 0) {
			System.out.println("输入天数错误!");
			rent(carlist);
		} else {
			fee = fee * day;
		}

		// 汇总账单信息
		System.out.println("您的账单:");
		System.out.println("***可载人的车有:");
		for (int i = 0; i < choose.length; i++) {
			if (carlist[choose[i] - 1].getContain_p() != 0) {
				System.out.print(carlist[choose[i] - 1].getCarname() + " ");
			}
		}
		if (totalcontain_p == 0) {
			System.out.println("没有可载人的车");
		} else {
			System.out.println("共载人:" + totalcontain_p + "人");
		}
		System.out.println("***可载货的车有:");
		for (int i = 0; i < choose.length; i++) {
			if (carlist[choose[i] - 1].getContain_g() != 0) {
				System.out.print(carlist[choose[i] - 1].getCarname() + " ");
			}
		}
		if (totalcontain_g == 0) {
			System.out.println("没有可载货的车");
		} else {
			System.out.println("共载货:" + totalcontain_g + "吨");
		}
		System.out.println("***租车总价格:" + fee + "元");
	}

}


正在回答

3 回答

package com.imooc.main;

import java.util.Scanner;

public class CarSystem {

	public static Scanner scanner;

	// 程序入口
	public static void main(String[] args) {
		CarSystem carSystem = new CarSystem();
		carSystem.isNeed();
	}

	// 判断是否需要进入系统
	public void isNeed() {
		System.out.println("欢迎使用达达租车系统:");
		System.out.println("您是否要租车:1是  0否");
		scanner = new Scanner(System.in);
		String input = scanner.next();
		if (input.equals("1")) {
			list();
		} else if (input.equals("0")) {
			System.out.println("退出系统,欢迎下次再来!");
		} else {
			System.out.println("输入错误!请输入1或0");
			isNeed();
		}
	}

	// 生成车辆列表
	public void list() {
		System.out.println("您可租车的类型及其价目表:");
		System.out.println("序号\t汽车名称\t租金\t容量");
		Vehicle audi = new Car(1, "奥迪A4", 500, 4);
		audi.say();
		Vehicle mazda = new Car(2, "马自达6", 400, 4);
		mazda.say();
		Vehicle pickup6 = new Pickup(3, "皮卡雪6", 450, 4, 2);
		pickup6.say();
		Vehicle jinlong = new Car(4, "金龙", 800, 20);
		jinlong.say();
		Vehicle songhuajiang = new Truck(5, "松花江", 400, 4);
		songhuajiang.say();
		Vehicle yiweike = new Truck(6, "依维柯", 1000, 20);
		yiweike.say();
		Vehicle[] carlist = { audi, mazda, pickup6, jinlong, songhuajiang,
				yiweike };
		rent(carlist);
	}

	// 选择车辆,同时计算租金和载人/载货数量
	public void rent(Vehicle[] carlist) {
		int fee = 0; // 总金额
		int totalcontain_p = 0; // 总载人数
		double totalcontain_g = 0; // 总载货量
		System.out.println("请输入您要租车的数量:");
		int num = scanner.nextInt();
		int[] choose = new int[num];
		if (num > 0 && num <= carlist.length) {
			for (int i = 1; i <= num; i++) {
				System.out.println("请输入第" + i + "辆车的序号:");
				int inputserial = scanner.nextInt();
				if (inputserial > 0 && inputserial <= carlist.length) {
					choose[i - 1] = inputserial;
					fee += carlist[inputserial - 1].getPrice();
					totalcontain_p += carlist[inputserial - 1].getContain_p();
					totalcontain_g += carlist[inputserial - 1].getContain_g();
				} else {
					System.out.println("输入错误,请输入1到" + carlist.length + "的数字!");
					rent(carlist);
					return;
				}
			}
			sum(carlist, fee, choose, totalcontain_p, totalcontain_g);
		} else {
			System.out.println("输入错误,请输入1到" + carlist.length + "的数字!");
			rent(carlist);
		}
	}

	// 计算总金额
	public void sum(Vehicle[] carlist, int fee, int[] choose,
			int totalcontain_p, double totalcontain_g) {
		System.out.println("请输入租车天数:");
		int day = scanner.nextInt();
		if (day <= 0) {
			System.out.println("输入天数错误!");
			sum(carlist, fee, choose, totalcontain_p, totalcontain_g);
		} else {
			fee = fee * day;
		}
		System.out.println("您的账单:");
		System.out.println("***可载人的车有:");
		for (int i = 0; i < choose.length; i++) {
			if (carlist[choose[i] - 1].getContain_p() != 0) {
				System.out.print(carlist[choose[i] - 1].getCarname() + " ");
			}
		}
		if (totalcontain_p == 0) {
			System.out.println("没有可载人的车");
		} else {
			System.out.println("共载人:" + totalcontain_p + "人");
		}
		System.out.println("***可载货的车有:");
		for (int i = 0; i < choose.length; i++) {
			if (carlist[choose[i] - 1].getContain_g() != 0) {
				System.out.print(carlist[choose[i] - 1].getCarname() + " ");
			}
		}
		if (totalcontain_g == 0) {
			System.out.println("没有可载货的车");
		} else {
			System.out.println("共载货:" + totalcontain_g + "吨");
		}
		System.out.println("***租车总价格:" + fee + "元");
		scanner.close();
	}
}

找出问题所在了,是因为循环中没有加return关键字导致重复调用了方法,改进了一下现在好了

0 回复 有任何疑惑可以回复我~

可以添加IO异常处理

0 回复 有任何疑惑可以回复我~
package com.imooc.main;

public abstract class Vehicle {
	protected int serial;
	protected String carname;
	protected double price;
	protected int contain_p;
	protected double contain_g;

	public String getCarname() {
		return carname;
	}

	public void setCarname(String carname) {
		this.carname = carname;
	}

	public int getSerial() {
		return serial;
	}

	public void setSerial(int serial) {
		this.serial = serial;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	
	public int getContain_p() {
		return contain_p;
	}

	public void setContain_p(int contain_p) {
		this.contain_p = contain_p;
	}

	public double getContain_g() {
		return contain_g;
	}

	public void setContain_g(double contain_g) {
		this.contain_g = contain_g;
	}

	public abstract void say();
	
}

class Car extends Vehicle {

	public Car(int serial,String carname, double price, int contain_p) {
		this.serial = serial;
		this.carname = carname;
		this.price = price;
		this.contain_p = contain_p;
	}

	@Override
	public void say() {
		System.out.println(getSerial()+".\t"+getCarname()+"\t"+getPrice()+"元/天 载人:"+getContain_p()+"人");
	}
}

class Truck extends Vehicle {

	public Truck(int serial,String carname, double price, int contain_g) {
		this.serial = serial;
		this.carname = carname;
		this.price = price;
		this.contain_g = contain_g;
	}

	@Override
	public void say() {
		System.out.println(getSerial()+".\t"+getCarname()+"\t"+getPrice()+"元/天 载货:"+getContain_g()+"吨");		
	}
}

class Pickup extends Vehicle {

	public Pickup(int serial,String carname, double price, int contain_p, double contain_g) {
		this.serial = serial;
		this.carname = carname;
		this.price = price;
		this.contain_p = contain_p;
		this.contain_g = contain_g;
	}

	@Override
	public void say() {
		System.out.println(getSerial()+".\t"+getCarname()+"\t"+getPrice()+"元/天 载人:"+getContain_p()+"人 载货"+getContain_g()+"吨");		
	}
}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

自己写了一个,貌似输入范围外数值后会引起流程混乱,请高手看下谢谢!

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号