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

嗯,发个代码供大家参考。

package common;
import java.util.Scanner;

public class SentCar {

	public static void main(String[] args) {

		int price = 0;
		int totalPeople = 0;
		int totalShop = 0;
		Car[] cars = new Car[3];
		cars[0] = new CarryPeople("擎天柱", 4, 300, 1);
		cars[1] = new CarryShop("大黄蜂", 20, 400, 2);
		cars[2] = new CarryAll("红蜘蛛", 4, 40, 800, 3);
		System.out.println("欢迎使用变形金刚租车系统");
		System.out.println("您是否要租车:1是 0否");
		Scanner scan = new Scanner(System.in);
		String str1 = scan.next();
		if(str1.equals("0")){
			System.out.println("欢迎下次光临!再见");
		}else if(str1.equals("1")){
			System.out.println("您可使用的租车类型和价格如下:");
			for (int i = 0;i < 3;i++){
				cars[i].printMessage(); 
			}
			System.out.println("请输入你要租车的数量:");
			//Scanner input = new Scanner(System.in); //scanner语句开辟的内存只有一个
			int number = scan.nextInt();
			String[] name = new String[number];
			Car[] selectCars = new Car[number];
			for(int i = 0;i < number;i++){
				System.out.println("请输入你要租的第"+ (i+1) +"车的类型");
				int type = scan.nextInt();
				if(type > 3| type < 0){
					System.out.println("输入不合法!");
				}else{
					System.out.println("第" + (i+1) +"辆车是" + cars[type - 1].name);
					price = price + cars[type - 1].price;
					name[i] = cars[type - 1].name;
					selectCars[i] = cars[type -1];
				}				
			}
			System.out.println("请选择你需要的租车天数:");
			int day = scan.nextInt();
			scan.close();
			price = price * day;
			System.out.println("您的账单是:");
			System.out.print("变形金刚系列中可载人的车有:" + "  ");
			for(int i = 0;i < number; i ++){
				if(selectCars[i].busload != 0){
					System.out.print(selectCars[i].name + "  ");
					totalPeople = totalPeople +  selectCars[i].busload;
				}
			}
			System.out.println("总载人量是:" + totalPeople + "人");
			System.out.print("变形金刚系列中可载货的车有:" + "  ");
			for(int i = 0;i < number; i ++){
				if(selectCars[i].cargo != 0){
					System.out.print(selectCars[i].name + "  ");
					totalShop = totalShop + selectCars[i].cargo;
				}
			}
			System.out.println("总载货量是:" + totalShop + "吨");
			System.out.println("您的租车总价格是"+ price +"元。");
		}

	}

}


正在回答

3 回答

楼主考虑一下:当所有参数放到父类中的时候,代表所有的子类均具有这些参数,也就是说,无论是载客还是载货的车都有载客参数和载货参数,这样还能正确体现两种车的区别么?

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

sioneden 提问者

噢,这个是后面修改的时候漏了的地方,多谢提醒
2014-10-15 回复 有任何疑惑可以回复我~

我是JAVA初学者,看了你的代码,受益良多,谢谢。

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

public abstract class Car {
	String name;
	int price;
	int number;
	int busload;
	int cargo;
	public void printMessage(){
	}
}

上面的是一个类

package common;

public class CarryPeople extends Car{
	
	public CarryPeople(){
	}
	public CarryPeople(String name, int people, int price, int number){
		this.name = name;
		this.busload = people;
		this.price = price;
		this.number = number;
	}
	public void printMessage(){
		System.out.println(number + "." + "\t" + name + "\t" + price + "元/天  \t" + "载人:" + busload + "人");
	}
}

接着一样

package common;

public class CarryShop extends Car{
	
	public CarryShop(){
	}
	public CarryShop(String name, int size, int price, int number){
		this.name = name;
		this.cargo = size;
		this.price = price;
		this.number = number;
	}
	public void printMessage(){
		System.out.println(number + ".\t" + name + "\t" + price + "元/天 \t " + "载货:" + cargo + "吨");
	}

}

还是一样

package common;

public class CarryAll extends Car{
	public CarryAll(){
	}
	public CarryAll(String name, int people,int size, int price, int number){
		this.name = name;
		this.cargo = size;
		this.busload = people;
		this.price = price;
		this.number = number;
	}
	public void printMessage() {
		// TODO Auto-generated method stub
		System.out.println(number + "." + "\t" + name + "\t" + price + "元/天  \t" + "载人:" + cargo + "人  载货:" + busload +"吨");
	}

}

写的比较繁琐,加了些检验,各位可以改进,发出自己的代码来交流

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

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信