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

第一版测试代码

/*
* 类 Car
*/
package Java;

public class Car {
	String carname;
	double price;
	double capacity;
	int nperson;
	
	public int getNperson() {
		return nperson;
	}
	public void setNperson(int nperson) {
		this.nperson = nperson;
	}
	public String getCarname() {
		return carname;
	}
	public void setCarname(String carname) {
		this.carname = carname;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public double getCapacity() {
		return capacity;
	}
	public void setCapacity(double capacity) {
		this.capacity = capacity;
	}
	
	public void setall(String carname,double price,int nperson,double capacity) {
		setCarname(carname);
		setPrice(price);
		setCapacity(capacity);
		setNperson(nperson);
	}
}

/*
* 类carDemo
*/
package Java;
import java.util.Scanner;
public class carDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("欢迎使用答答租车系统!");
		System.out.println("您是否要租车?1是,0否");
		Scanner in=new Scanner(System.in);
		int op=in.nextInt();
		if(op==0)return;
		if(op==1){
			Car aodiA4=new Car();
			Car mazida=new Car();
			Car pikaxue=new Car();
			Car jinlong=new Car();
			Car songhuajiang=new Car();
			Car yiweihe=new Car();
			aodiA4.setall("奥迪A4", 500, 4, 0);
			mazida.setall("马自达6", 400, 4, 0);
			pikaxue.setall("皮卡雪6", 450, 4, 2);
			jinlong.setall("金龙", 800, 20, 0);
			songhuajiang.setall("松花江", 400, 0, 4);
			yiweihe.setall("依维河", 1000, 0, 20);
			System.out.println("您可以租车的类型及价目表:");
			System.out.println("序号   汽车名称     租金     人数   容量  ");
			System.out.println("1  "+aodiA4.getCarname()+"  "+aodiA4.getPrice()+"  "+aodiA4.getNperson()+"  "+aodiA4.getCapacity());
			System.out.println("2  "+mazida.getCarname()+"  "+mazida.getPrice()+"  "+mazida.getNperson()+"  "+mazida.getCapacity());
			System.out.println("3  "+pikaxue.getCarname()+"  "+pikaxue.getPrice()+"  "+pikaxue.getNperson()+"  "+pikaxue.getCapacity());
			System.out.println("4  "+jinlong.getCarname()+"  "+jinlong.getPrice()+"  "+jinlong.getNperson()+"  "+jinlong.getCapacity());
			System.out.println("5  "+songhuajiang.getCarname()+"  "+songhuajiang.getPrice()+"  "+songhuajiang.getNperson()+"  "+songhuajiang.getCapacity());
			System.out.println("6  "+yiweihe.getCarname()+"  "+yiweihe.getPrice()+"  "+yiweihe.getNperson()+"  "+yiweihe.getCapacity());
		}
		System.out.println("请输入你要租车的数量:");
		Scanner in2=new Scanner(System.in);
		int num=in2.nextInt();
		if(num<1)return;
		int carnum[]=new int[num];
		for(int i=0;i<num;i++){
			System.out.println("请输入第"+(i+1)+"辆车的序号:");
			Scanner in3=new Scanner(System.in);
			carnum[i]=in3.nextInt();
		}
		System.out.println("请输入你要租车的天数:");
		Scanner in4=new Scanner(System.in);
		int day=in4.nextInt();
		
		System.out.println("您的租车账单为:");
		printall(carnum,day);	
	}
	public static void printall(int carnum[],int day){
		double priceall=0;
		int pnum=0;
		double tnum=0;
		String name[]=new String[carnum.length];
		String capacityname="";
		String peoplename="";
		Car aodiA4=new Car();
		Car mazida=new Car();
		Car pikaxue=new Car();
		Car jinlong=new Car();
		Car songhuajiang=new Car();
		Car yiweihe=new Car();
		aodiA4.setall("奥迪A4", 500, 4, 0);
		mazida.setall("马自达6", 400, 4, 0);
		pikaxue.setall("皮卡雪6", 450, 4, 2);
		jinlong.setall("金龙", 800, 20, 0);
		songhuajiang.setall("松花江", 400, 0, 4);
		yiweihe.setall("依维河", 1000, 0, 20);
		for(int i=0;i<carnum.length;i++){
			int acarnum=carnum[i];
			switch (acarnum){
				case 1:
					priceall=priceall+aodiA4.getPrice();
					pnum=pnum+aodiA4.getNperson();
					tnum=tnum+aodiA4.getCapacity();					
					peoplename=peoplename+aodiA4.getCarname()+"  ";	
					break;
				case 2:
					priceall=priceall+mazida.getPrice();
					pnum=pnum+mazida.getNperson();
					tnum=tnum+mazida.getCapacity();					
					peoplename=peoplename+mazida.getCarname()+"  ";	
					break;
				case 3:
					priceall=priceall+pikaxue.getPrice();
					pnum=pnum+pikaxue.getNperson();
					tnum=tnum+pikaxue.getCapacity();					
					peoplename=peoplename+pikaxue.getCarname()+"  ";
					capacityname=capacityname+pikaxue.getCarname()+"  ";
					break;
				case 4:
					priceall=priceall+jinlong.getPrice();
					pnum=pnum+jinlong.getNperson();
					tnum=tnum+jinlong.getCapacity();					
					peoplename=peoplename+jinlong.getCarname()+"  ";	
					break;
				case 5:
					priceall=priceall+songhuajiang.getPrice();
					pnum=pnum+songhuajiang.getNperson();
					tnum=tnum+songhuajiang.getCapacity();					
					capacityname=capacityname+songhuajiang.getCarname()+"  ";
					break;
				case 6:
					priceall=priceall+yiweihe.getPrice();
					pnum=pnum+yiweihe.getNperson();
					tnum=tnum+yiweihe.getCapacity();
					capacityname=capacityname+yiweihe.getCarname()+"  ";
					break;
				default:
					break;
			}
		}
		
		System.out.println("**可载人的车有");
		System.out.println(peoplename+"共载客"+pnum+"人");
		System.out.println("**可载货的车有");
		System.out.println(capacityname+"共载货"+tnum+"吨");
		System.out.println("租车总价格"+priceall*day+"元");
		
	}
}


正在回答

3 回答

没有打印出总的载货量和人数

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

举报

0/150
提交
取消
Java入门第二季 升级版
  • 参与学习       530552    人
  • 解答问题       6091    个

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

进入课程

第一版测试代码

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信