package com.imooc; import java.util.Scanner; public class Entry { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("-- 您好!欢迎进入大大租车系统!"); //提示客户是否租车 System.out.println("您是否需要租车?(1:是\t2:否)"); Scanner in = new Scanner(System.in); int ys = in.nextInt(); int days = 1; int num = 1; int sum = 0; String cname = ""; int cload = 0; int csize = 0; if(ys==1){ System.out.println("可租汽车的类型及价目"); System.out.println("序号\t名称\t租金\t\t容量"); Automobile truck = new Truck(); Automobile van = new Van(); Automobile coach = new Coach(); truck.showDetails(); van.showDetails(); coach.showDetails(); //流程 System.out.println("请输入您要租车的数量:"); num = in.nextInt(); for(int i=1;i<=num;i++){ System.out.println("请输入您要租的第"+i+"辆车的序号:"); int cid = in.nextInt(); if(cid>0&&cid<6){ switch(cid){ case 1: sum+=truck.price[1]; cload+=((Truck)truck).load[1]; cname+=truck.name[1]+"、"; break; case 2: sum+=truck.price[2]; cload+=((Truck)truck).load[2]; cname+=truck.name[2]+"、"; break; case 3: sum+=van.price[3]; cload+=((Van)van).load[3]; csize+=((Van)van).size[3]; cname+=van.name[3]+"、"; break; case 4: sum+=coach.price[4]; csize+=((Coach)coach).size[4]; cname+=coach.name[4]+"、"; break; case 5: sum+=coach.price[5]; csize+=((Coach)coach).size[5]; cname+=coach.name[5]+"、"; break; } } } System.out.println("请输入您要租几天:"); days = in.nextInt(); System.out.println("-------以下为租车详情-------"); System.out.println("共租"+num+"辆车,分别有:"+cname); System.out.println("总载重"+cload+"T,总载人数"+csize+"人,共租"+days+"天,总金额为"+(sum*days)+"元。"); } } }
package com.imooc; //父类定义 public abstract class Automobile { int[] id = {0,1,2,3,4,5}; String[] name = {"","松花江","依维柯","皮卡雪","奥迪A4","金龙"}; int[] price = {0,400,1000,450,500,800}; public abstract void showDetails(); }
package com.imooc; //货车 public class Truck extends Automobile implements ILoad{ public void showDetails() { System.out.println(id[1]+"\t"+name[1]+"\t"+price[1]+"元/天\t\t载货"+load[1]+"T"); System.out.println(id[2]+"\t"+name[2]+"\t"+price[2]+"元/天\t\t载货"+load[2]+"T"); } }
package com.imooc; //皮卡 public class Van extends Automobile implements ILoad,ISize{ public void showDetails() { System.out.println(id[3]+"\t"+name[3]+"\t"+price[3]+"元/天\t\t载货"+load[3]+"T,载人"+size[3]); } }
package com.imooc; //客车 public class Coach extends Automobile implements ISize{ public void showDetails() { System.out.println(id[4]+"\t"+name[4]+"\t"+price[4]+"元/天\t\t载人"+size[4]); System.out.println(id[5]+"\t"+name[5]+"\t"+price[5]+"元/天\t\t载人"+size[5]); } }
package com.imooc; //载重 public abstract interface ILoad { int[] load = {0,4,20,4}; }
package com.imooc; //载人数 public abstract interface ISize { int[] size = {0,0,0,4,4,20}; }
可以通过while里面镶嵌if else的方式配合break来确认
//流程 System.out.println("请输入您要租车的数量:"); num = in.nextInt(); for(int i=1;i<=num;i++){ System.out.println("请输入您要租的第"+i+"辆车的序号:"); int cid = in.nextInt(); while(true){//更改处 if(cid>0&&cid<6){ switch(cid){ case 1: sum+=truck.price[1]; cload+=((Truck)truck).load[1]; cname+=truck.name[1]+"、"; break; case 2: sum+=truck.price[2]; cload+=((Truck)truck).load[2]; cname+=truck.name[2]+"、"; break; case 3: sum+=van.price[3]; cload+=((Van)van).load[3]; csize+=((Van)van).size[3]; cname+=van.name[3]+"、"; break; case 4: sum+=coach.price[4]; csize+=((Coach)coach).size[4]; cname+=coach.name[4]+"、"; break; case 5: sum+=coach.price[5]; csize+=((Coach)coach).size[5]; cname+=coach.name[5]+"、"; break; } break; //更改处 } else{ //更改处 System.out.println("大哥你输入的数有误诶!重新输入吧!"); //更改处 } //更改处 ) //更改处 }
试试枚举类型如何?