2302234023
2015-09-05 17:35
//Car.java package com.dadazuche; public abstract class Car { protected int num; protected String name; protected int rent; public abstract void show(); public String getName() { return name; } } //Bus.java package com.dadazuche; public class Bus extends Car { private int pC;//此处pC为passenger capacity的简写,表示载客量 public Bus(int newNum,String newName,int newRent,int newPC){ num=newNum; name=newName; rent=newRent; pC=newPC; } public void show(){ System.out.println(num+"."+" "+name+" "+rent+"元/天 载人:"+pC+"人"); } } //Truck.java package com.dadazuche; public class Truck extends Car { private int cC;//此处pC为cargo capacity的简写,表示载货量 public Truck(int newNum,String newName,int newRent,int newCC){ num=newNum; name=newName; rent=newRent; cC=newCC; } public void show(){ System.out.println(num+"."+" "+name+" "+rent+"元/天 载货:"+cC+"吨"); } } //PickUp.java package com.dadazuche; public class PickUp extends Car { private int pC;//此处pC为passenger capacity的简写,表示载客量 private int cC;//此处pC为cargo capacity的简写,表示载货量 public PickUp(int newNum,String newName,int newRent,int newPC,int newCC){ num=newNum; name=newName; rent=newRent; pC=newPC; cC=newCC; } public void show(){ System.out.println(num+"."+" "+name+" "+rent+"元/天 载人:"+pC+"人 载货:"+cC+"吨"); } } //Initial.java package com.dadazuche; import java.util.*; public class Initial { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner input=new Scanner(System.in); System.out.println("欢迎使用答答租车系统:"+'\n'+"您是否要租车:1是 0否"); int b=input.nextInt(); if(b==1){ System.out.println("您可租车的类型及其价目表:"+'\n'+"序号 汽车名称 租金 容量"); Car[] carArray={new Bus(1,"奥迪A4",500,4),new Bus(2,"马自达6",400,4),new PickUp(3,"皮卡雪6",450,4,2), new Bus(4,"金龙 ",800,20),new Truck(5,"松花江 ",400,4),new Truck(6,"依维柯 ",1000,20)}; for(Car car:carArray) car.show(); System.out.println("请输入您要租汽车的数量:"); int cNum=input.nextInt(); int s1,s2,s3;//s1总载客量,s2总载货量,s3租一天的总价格 s1=s2=s3=0; int[] arr=new int[cNum];//将用户输入的序号保存到数组arr中 for(int i=1;i<=cNum;i++){ System.out.println("请输入第"+i+"辆车的序号:"); arr[i-1]=input.nextInt(); switch(arr[i-1]){ case 1: s1+=4; s3+=500; break; case 2: s1+=4; s3+=400; break; case 3: s1+=4; s2+=2; s3+=450; break; case 4: s1+=20; s3+=800; break; case 5: s2+=4; s3+=400; break; case 6: s2+=20; s3+=1000; break; default: System.out.println("输入序号有误,请重新输入!"); i--; break; } } System.out.println("请输入租车天数:"); int d=input.nextInt(); System.out.println("您的账单:"); /* * 以下的双重for循环主要是保证输出车名时不会有重复的情况出现 * 水平有限,如有更好方法还请赐教 */ System.out.println("***可载人的车有:"); for(int i=0;i<cNum;i++){ boolean p=true; for(int j=i+1;j<cNum;j++){ if(arr[j]==arr[i]){p=false;break;} } if(p&&arr[i]<=4)System.out.print(carArray[arr[i]-1].getName()+'\t'); } System.out.println("共载人:"+s1+"人"); System.out.println("***载货的车有:"); for(int i=0;i<cNum;i++){ boolean p=true; for(int j=i+1;j<cNum;j++){ if(arr[j]==arr[i]){p=false;break;} } if(p&&(arr[i]>4||arr[i]==3))System.out.print(carArray[arr[i]-1].getName()+'\t'); } System.out.println("共载货:"+s2+"吨"); System.out.println("***租车总价格:"+s3*d+"元"); }else if(b==0)System.out.println("已退出答答租车系统,谢谢访问!"); else System.out.println("输入错误!"); } }
整体写的不错啊,我参考你的写的。不过,switch case 语句那样控制不好,上面已经初始化对象了,为何不调用车辆的相关属性输出呢?
前面定义了对象,怎么后面计算不用上对应的属性,直接写数字~
还不错哦
Java入门第二季 升级版
530557 学习 · 6091 问题
相似问题