Supper学渣
2015-07-16 22:00
/*所有汽车父类*/ public abstract class Che { public String chename; public double zaihuoliang; public int zairenliang; public int money; Che(){ } void xianshi(){ } } /*货车类*/ public class Huoche extends Che{ Huoche(String chename,double zaihuo,int money){ this.chename = chename; zaihuoliang = zaihuo; this.money = money; } void xianshi(){ System.out.println("货车型号:"+chename+" 载货量:"+zaihuoliang+"吨 出租价"+money); } } /*客车类*/ public class Keche extends Che{ Keche(String chename,int zairen,int money){ this.chename = chename; zairenliang = zairen; this.money = money; } void xianshi(){ System.out.println("客车型号:"+chename+" 载人量:"+zairenliang+"人 出租价"+money); } } /*皮卡类*/ public class Pika extends Che{ Pika(String chename,int zairen,double zaihuo,int money){ this.chename = chename; zairenliang = zairen; zaihuoliang = zaihuo; this.money = money; } void xianshi(){ System.out.println("皮卡型号:"+chename+" 载货量:"+zaihuoliang+"吨 载人量:"+zairenliang+"人 出租价"+money); } } /*主类*/ import java.util.Scanner; public class Test extends Che{ public static void main(String[] args) { int anniu; int zu,money=0,zairen=0; double zaihuo=0.0; Scanner shuru = new Scanner(System.in); do{ System.out.println("欢迎来到租车系统,你是来租车的吗?(1.是 2.否)"); anniu = shuru.nextInt(); if(anniu!=1) System.out.println("来都来了,再考虑考虑吧"); }while(anniu!=1); Che c1 = new Huoche("大货车",20.0,1000); Che c2 = new Huoche("小货车",5.0,500); Che c3 = new Keche("大客车",40,1000); Che c4 = new Keche("小客车",10,500); Che c5 = new Pika("宝马皮卡车",5,3.0,1000); Che c6 = new Pika("屌丝皮卡车",2,2.0,100); System.out.println("1."); c1.xianshi(); System.out.println("2."); c2.xianshi(); System.out.println("3."); c3.xianshi(); System.out.println("4."); c4.xianshi(); System.out.println("5."); c5.xianshi(); System.out.println("6."); c6.xianshi(); do{ System.out.println("以上是可以租的车辆,请问你要租几辆?(输入数字)"); anniu = shuru.nextInt(); }while(anniu<=0); int []xinghao=new int[anniu+1]; System.out.println("你要租"+anniu+"辆车,请依次按下车辆序号租车:"); Scanner shuru2=new Scanner(System.in); int i=0; do{ i=i+1; do{ System.out.println("第"+i+"辆"); zu = shuru2.nextInt(); switch(zu){ case 1:money=money+1000;zairen=zairen+0;zaihuo=zaihuo+20.0;xinghao[i]=1;break; case 2:money=money+500;zairen=zairen+0;zaihuo=zaihuo+5.0;xinghao[i]=2;break; case 3:money=money+1000;zairen=zairen+40;zaihuo=zaihuo+0.0;xinghao[i]=3;break; case 4:money=money+500;zairen=zairen+10;zaihuo=zaihuo+0.0;xinghao[i]=4;break; case 5:money=money+1000;zairen=zairen+5;zaihuo=zaihuo+3.0;xinghao[i]=5;break; case 6:money=money+500;zairen=zairen+2;zaihuo=zaihuo+2.0;xinghao[i]=6;break; }; }while(zu>=7||zu<=0); }while(i!=anniu); System.out.println("你租了"+anniu+"辆车,你的清单如下:"); System.out.println("租车总金额:"+money); System.out.println("租车总载货量:"+zaihuo+"吨"); System.out.println("租车总载人量:"+zairen+"人"); System.out.println("车型为下:"); for(int j=1;j<=anniu;j++){ switch(xinghao[j]){ case 1:System.out.print(j+".大型货车");break; case 2:System.out.print(j+".小型货车");break; case 3:System.out.print(j+".大型客车");break; case 4:System.out.print(j+".小型客车");break; case 5:System.out.print(j+".宝马皮卡车");break; case 6:System.out.print(j+".屌丝皮卡车");break; } } System.out.println("\n"); System.out.println("是否确定租车?(1.是 , 2.否)"); anniu=shuru2.nextInt(); if(anniu==1) System.out.println("租车成功,谢谢你的光临"); else System.out.println("BOOM,系统崩溃了叫你乱按"); } }
建议每辆车的资料信息不要写在主函数里,直接写在每个类的构造函数里最好,在主函数里只写操作就可以
就是一个简单的类的一个小例子,根据你主函数里的流程依次下来,不难理解的就是要你选车辆,先是把所有车辆罗列出来供你选,选好车辆后,会将车辆的信息显示出来,仅此而已。注意一下那些输出语句,程序里的输出提示很有帮助
Java入门第二季 升级版
530559 学习 · 6091 问题
相似问题