花了好长时间才做出来,求大神指教有什么不对的或者可以优化的地方

来源:12-2 项目问题解析 1

慕粉1844174621

2020-05-27 14:54

一开始看到不止的题目没有头绪,参照了前辈们的答案才做出来,修修改改了好久总算达成了跟课程老师的题目差不多的要求,和自己加的一些交互代码。

帮小弟提下意见或者建议 谢谢啦

package com.imooc;
/*车 父类*/
public class Car { 
	protected  String carName; //车名
	protected  int money; //价格
	protected  int seatNum; //座位数
	protected  double weigthNum; //载货量 直接用了double类型
	private  double toMoney=0; //总价格
	private  int toSeat=0; //总座位
	private  int toWeigth=0; //总载重
	private int day=1; //租车天数
	
	public Car(String newCarName,int newMoney,int newSeatNum,double NewWeigthNum) {
            this.carName=newCarName;
	  this.money=newMoney;		
	  this.seatNum=newSeatNum;		
	  this.weigthNum=NewWeigthNum;
	 }
	 
	public Car() {
	}
	
	public String getCarName() {
	    return carName;
	}
	
	public int getMoney() {
	    return money;
	}
	
	public int getSeatNum() {
	    return seatNum;
	}
	
	public double getWeigthNum() {
	    return weigthNum;
	}
	
	public double getToMoney() {
	    return toMoney;	
	}
	
	public int getToSeat() {
	    return toSeat;	
	}
	
	public int getToWeigth() {
	    return toWeigth;
	}
	
	public int getDay() {
	    return day;	
	}
	
	public void show() {
		if(weigthNum<=0) {
			System.out.println("\t"+carName+"\t"+money+"元/天\t载人:"+seatNum+"人");
		}		
		else if(seatNum<=0) {
			System.out.println("\t"+carName+"\t"+money+"元/天\t载货:"+weigthNum+"吨");
		}else {			
		System.out.println("\t"+carName+"\t"+money+"元/天\t载人:"+seatNum+"人 载货:"+weigthNum+"吨");		
		}	
	}	
}
package com.imooc;
/*客车 子类*/
public class PassCar extends Car {
	public PassCar(String newCarName,int newMoney,int newSeatNum){
            super.carName=newCarName;
            super.money=newMoney;
            super.seatNum=newSeatNum;
        }
}
package com.imooc;
/*皮卡车 子类*/
public class PickupCar extends Car {
    public PickupCar(String newCarName,int newMoney,int newSeatNum,double weigthNum){
        super.carName=newCarName;		
        super.money=newMoney;		
        super.seatNum=newSeatNum;		
        super.weigthNum=weigthNum;	
        }	
}
package com.imooc;
/*货车 子类*/
public class TruckCar extends Car {		
    public TruckCar(String newCarName,int newMoney,double weigthNum){		
        super.carName=newCarName;		
        super.money=newMoney;		
        super.weigthNum=weigthNum;	
        }
} 	


  1. package com.imooc;

  2. import java.util.*;

  3. /*运行类*/

  4. public class Initail {

  5. public static void main(String[] args) {

  6. Scanner input = new Scanner(System.in);

  7. System.out.println("***欢迎使用答答租车系统***");

  8. System.out.println("您是否要租车?输入数字\t1是,2否"); //判断是否要租车

  9. Car carVa=new Car();

  10. int toSeat=carVa.getToSeat(); //定义车总座位数

  11. double toWeigth=carVa.getWeigthNum(); //定义值车总载重数

  12. double toMoney=carVa.getMoney(); //定义总价格 

  13. int day=carVa.getDay();

  14. int carType=0; //定义选择的车序号

  15. int maxC=999; //定义最大租车数量


  16. for(int tf=3;tf>=0;tf--) { 

  17. int confirmInt=input.nextInt(); //输入数字判断是否要租车


  18. if(confirmInt==1) {

  19. tf=-1; //结束外层租车需求循环判断 18行


  20. Car car[]={

  21. new PassCar("奥迪A4",500,4),

  22. new PassCar("马自达6",400,4),

  23. new PassCar("金龙",800,20),

  24. new PickupCar("皮卡雪6",450,4,2),

  25. new TruckCar("松花江",400,4),

  26. new TruckCar("依维柯",1000,20),

  27. };


  28. System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");

  29. for(int c=0,d=1;c<car.length;c++,d++) { //显示租车列表

  30. System.out.print(d+".");

  31. car[c].show();

  32. }


  33. int carNum=3; //定义租车数量

  34. System.out.println("请输入您要租车的数量");

  35. for(int tfNum=3;tfNum>=0;tfNum--) { //循环最大租车数量

  36. carNum=input.nextInt(); 

  37. String carN[]=new String[carNum]; //定义数组,保存选择后的车辆-车名

  38. int carS[]=new int[carNum]; //定义数组,保存选择后的车辆-座位数

  39. double carW[]=new double[carNum]; //定义数组,保存选择后的车辆-载重数

  40. List<String> listS = new ArrayList<String>(); 

  41. List<String> listW = new ArrayList<String>(); 

  42. if(carNum>0&&carNum<=maxC) { //判断租车数量.小于max=999, 进入选车循环

  43. tfNum=-1;


  44. for(int i=1,tfNo=3;i<=carNum;) {

  45. System.out.println("请输入第"+i+"辆车的序号,输入"+"[ 0 ]"+"直接进入下一步租车天数界面");

  46. carType=input.nextInt(); //选择车序号

  47. if(carType<=car.length&&carType>0) { //如果输入的车序号小于车辆数据总长度

  48. tfNo=3;

  49. toMoney=toMoney+car[carType-1].getMoney(); //计算总价格 

  50. toSeat=toSeat+car[carType-1].getSeatNum(); //计算总座位/载人数

  51. toWeigth=toWeigth+car[carType-1].getWeigthNum(); //计算总载重数

  52. carS[i-1]=car[carType-1].getSeatNum(); //保存我们选择的车座位数为数组

  53. carW[i-1]=car[carType-1].getWeigthNum();//保存我们选择的车载重为数组

  54. carN[i-1]=car[carType-1].getCarName(); //保存我们选择的车名为数组

  55. i++;

  56. if(i<carNum) {

  57. //显示租车列表

  58. System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");

  59. for(int c=0,d=1;c<car.length;c++,d++) { 

  60. System.out.print(d+".");

  61. car[c].show();

  62. }

  63. }

  64. }

  65. else {

  66. if(tfNo>0&&carType!=0) {

  67. //显示租车列表

  68. System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");

  69. for(int c=0,d=1;c<car.length;c++,d++) { 

  70. System.out.print(d+".");

  71. car[c].show();

  72. }

  73. System.out.println();

  74. System.out.println("输入错误!此序号的车辆不存在,还可输入"+tfNo+"次。");

  75. tfNo--;

  76. }

  77. else if(tfNo<=0) {

  78. System.out.println("输入的租车数量,错误次数已达3次,退出租车界面");

  79. break;

  80. }

  81. }

  82. //判断车辆完选择之后

  83. if(i>carNum||carType==0) { 


  84. System.out.println("请输入租车天数:");

  85. day=input.nextInt();

  86. if(day>0) { //判断租车天数

  87. System.out.println("您的账单:");

  88. System.out.println("***可载人的车有:");

  89. for(int s=0;s<carN.length;s++) {

  90. if(!listS.contains(carN[s])) { //判断重复值

  91. if(carS[s]>0) { //判断座位数大于0

  92. listS.add(carN[s]); //加到list

  93. }

  94. }

  95. }

  96. System.out.print(listS); //输出去重复值后的车

  97. if(toSeat<=0) { //判断有没有选择客车

  98. System.out.println("无");

  99. }else {

  100. System.out.println("\t总载人:"+toSeat+"人");

  101. }

  102. System.out.println("***可载货的车有:");

  103. for(int w=0;w<carN.length;w++) {

  104. if(!listW.contains(carN[w])) { //判断重复值

  105. if(carW[w]>0) { //判断座位数大于0

  106. listW.add(carN[w]); //加到list

  107. }

  108. }

  109. }

  110. System.out.print(listW); //输出 去重复值 后的车

  111. if(toWeigth<=0) { //判断有没有选择货车

  112. System.out.println("无");

  113. }else {

  114. System.out.println("\t总载货:"+toWeigth+"吨");

  115. }

  116. System.out.println("租车总价格:"+toMoney*day+"元");

  117. }else {

  118. int tfDay=3;

  119. System.out.println("租车天数错误,请重新输入:");

  120. while(tfDay>=0) {

  121. day=input.nextInt();

  122. tfDay--;

  123. }

  124. if(tfDay<=0) {

  125. System.out.println("输入租车天数错误次数已达最大值,默认给您选择租车天数为1天");

  126. day=1;

  127. }

  128. }

  129. if(carType==0) {

  130. break;

  131. }

  132. }

  133. }

  134. }

  135. else{

  136. if(tfNum>0) {

  137. System.out.println("输入错误!租车数量需要在1-"+maxC+"之间,还可输入"+tfNum+"次。");

  138. System.out.println("请重新输入租车数量:");

  139. }

  140. else if(tfNum<=0) {

  141. System.out.println("输入的租车数量,错误次数已达3次,退出租车界面");

  142. break;

  143. }

  144. }

  145. }

  146. }

  147. else if(confirmInt==2) {

  148. System.out.println("您选择了否,感谢使用!");

  149. break;

  150. }

  151. else if(confirmInt!=1||confirmInt!=2){

  152. if(tf>0) {

  153. System.out.println("输入错误!还可输入"+tf+"次。");

  154. System.out.println("输入数字选择您是否要租车? 1是,2否");

  155. }

  156. else if(tf<=0) {

  157. System.out.println("输入需求错误次数已到达3次,退出租车界面。");

  158. }

  159. }

  160. }

  161. }


  162. }

写回答 关注

4回答

  • Du1in9
    2020-07-21 22:17:37

    欢迎参考我的学习笔记,包括第一季到第三季全部代码~

    https://www.jianshu.com/p/43f45dd0b22f

  • weixin_慕娘4423187
    2020-07-12 22:28:43

    真长长

  • 慕函数4309305
    2020-07-11 17:27:52

    package java22;

    import java.util.*;

    import java.util.Scanner; 

    public class RentCar {

    public static void main(String[] args) {

    System.out.println("欢迎使用答答租车系统:");

    System.out.println("您是否要租车:1是 0否");

    boolean withDraw2 = true,withDraw3 = true,withDraw4 = true;//输入异常时退出用

    boolean isprint1= false,

    isprint2= false,

    isprint3_1= false,

    isprint3_2 = false,

    isprint4= false,

    isprint5= false,

    isprint6= false;

    Scanner strRent = new Scanner(System.in); //第1次输入,是否租车

    if(strRent.hasNext()){   

    String Rent = strRent.nextLine();

    if(Rent.equals("1")){

            System.out.println("您可租车的类型及其价目表:");

            System.out.println("序号    汽车名称        租金         容量");

            System.out.println("1.   奥迪A4       500元/天   载人:4人");//载人 1-4人,2-4人,3-4人,4-20人

            System.out.println("2.   马自达6   400元/天   载人:4人");//载货 3-2吨,5-4吨,6-20吨

            System.out.println("3.   皮卡雪6      450元/天    载人:4 载货:2吨");

            System.out.println("4.   金龙   800元/天   载人:20人");

            System.out.println("5.   松花江            400元/天   载货:4吨");

            System.out.println("6.   依维柯            1000元/天 载货:20吨");

            System.out.println("请输入您要租汽车的数量:");               

            Scanner strRentNumber = new Scanner(System.in); //第2次输入,租几辆

            if(strRentNumber.hasNext()){

            String RentNumber = strRentNumber.nextLine();

            for (int i = 0; i < RentNumber.length(); i++){

            if(withDraw2 == true){

            if (!Character.isDigit(RentNumber.charAt(i))){

            System.out.println("非法输入!");

            withDraw2 = false;

            break;

            }

            }else{break;}

            }

            if(withDraw2 == true){

            int rentNumber = Integer.parseInt(RentNumber);       

            int[] rentCar = new int[rentNumber + 1];

            for(int i = 0; i<rentNumber+1;i++){

            rentCar[i] = 0;

            }

            int j = 1,i = 1;

            while(j <= rentNumber){

            if(withDraw3 == true){

            System.out.println("请输入第"+j+"辆车的序号:");

            Scanner strCarType = new Scanner(System.in);//第3次输入,租哪辆车

            if(strCarType.hasNext()){

            String CarType = strCarType.nextLine();

            for (i = 0; i < CarType.length(); i++){       

                    if (!Character.isDigit(CarType.charAt(i))){

                    System.out.println("非法输入!");

                    withDraw3 = false;

                    break;

                    }

                    }

                   if(withDraw3 == true){

            int carType = Integer.parseInt(CarType);

            rentCar[j] = carType;

            if(rentCar[j] < 1 || rentCar[j] > 6)

                { 

            System.out.println("非法输入!");

            System.out.println( "没有" + rentCar[j] + "型车。");

            System.out.println("再见!");

            withDraw3 = false;

            }

                   }

                   }

           

            }else{break;}

            j++;

            }

            if(withDraw3 == true){

            double cost = 0.0,load = 0.0;

            int manned = 0;

            for(i = 1; i <= rentNumber;i++){

            if(rentCar[i] > 0){

            switch (rentCar[i]){

            case 0:break;

            case 1:cost += 500;manned += 4;break;

            case 2:cost += 400;manned += 4;break;

            case 3:cost += 450;manned += 4;load += 2;break;

            case 4:cost += 800;manned += 20;break;

            case 5:cost += 400;load += 4;break;

            case 6:cost += 1000;load += 20;break;

            default:break;

            }

               }

            }

            System.out.println("请输入租车天数:");//第4次输入,租车天数

            Scanner strDays = new Scanner(System.in); 

            if(strDays.hasNext()){

            String RentDays = strDays.nextLine();

            for ( i = 0; i < RentDays.length(); i++){       

                if (!Character.isDigit(RentDays.charAt(i))){

                System.out.println("非法输入!");

                withDraw4 = false;

                break;

                }

                }

            if(withDraw4 == true){

            int rentDays = Integer.parseInt(RentDays); 

            cost *= rentDays;

            }

            }

            if(withDraw4 == true){

            System.out.println("您的账单:");

            System.out.println("**可载人的车有:");

            if(manned == 0){

            System.out.println("无");

            }

            else{         

              for(i = 1;i <= rentNumber;i++){

            if(rentCar[i] > 0){

            if (rentCar[i] == 1||rentCar[i] == 2||rentCar[i] == 3||rentCar[i] == 4){

            switch(rentCar[i]){

            case 1:

            if(isprint1 == false){

            System.out.print("奥迪A4" + " ");

            isprint1 = true;

            break;

            }

            case 2:

            if(isprint2 == false){

            System.out.print("马自达6" + " ");

            isprint2 = true;

            break;

            }

           

            case 3:

            if(isprint3_1 == false){

            System.out.print("皮卡雪6" + " ");

            isprint3_1 = true;

            break;

            }       

            case 4:

            if(isprint4 == false){

            System.out.print("金龙" + " ");

            isprint4 = true;

            break;

            }             

            default:break;

            }

            }

            }       

            }

            }

            System.out.println("共载人:" + manned);

            System.out.println("**载货的车有:");

            if(load == 0){

            System.out.println("无");

            }

            else{

              for(i = 1;i <= rentNumber;i++){

            if(rentCar[i] > 0){

            if (rentCar[i] == 3||rentCar[i] == 5||rentCar[i] == 6){

            switch(rentCar[i]){

            case 3:

            if(isprint3_2 == false){

            System.out.print("皮卡雪6" + " ");

            isprint3_2 = true;

            break;

            }               

            case 5:

            if(isprint5 == false){

            System.out.print("松花江" + " ");

            isprint5 = true;

            break;

            }       

            case 6:

            if(isprint6 == false){

            System.out.print("依维河" + " ");

            isprint6 = true;

            break;

            }       

            default:break;

            }

            }

              }       

               }

            }

            System.out.println("共载货:" + load + "吨");

            System.out.println("**租车总价格:" + cost +"元");

            System.out.println("账单计算结束.感谢您的光临,祝您生活愉快!");

            }

            }

            }

            }

    }else if(Rent.equals("0"))

      {

    System.out.println("ByeBye!");

      }else{

      System.out.println("非法输入!再见!");

      }       

    }

    }

    }


    我来粘个非对象程序的

  • 慕用9282841
    2020-06-19 11:23:17

    哇,真长

Java入门第二季 升级版

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

530105 学习 · 6086 问题

查看课程

相似问题