花了好長時間才做出來,求大神指教有什么不對的或者可以優(yōu)化的地方
一開始看到不止的題目沒有頭緒,參照了前輩們的答案才做出來,修修改改了好久總算達成了跟課程老師的題目差不多的要求,和自己加的一些交互代碼。
幫小弟提下意見或者建議 謝謝啦
package?com.imooc; /*車?父類*/ public?class?Car?{? protected??String?carName;?//車名 protected??int?money;?//價格 protected??int?seatNum;?//座位數(shù) protected??double?weigthNum;?//載貨量?直接用了double類型 private??double?toMoney=0;?//總價格 private??int?toSeat=0;?//總座位 private??int?toWeigth=0;?//總載重 private?int?day=1;?//租車天數(shù) 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; ????????} }?
package com.imooc;
import java.util.*;
/*運行類*/
public class Initail {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("***歡迎使用答答租車系統(tǒng)***");
System.out.println("您是否要租車?輸入數(shù)字\t1是,2否"); //判斷是否要租車
Car carVa=new Car();
int toSeat=carVa.getToSeat(); //定義車總座位數(shù)
double toWeigth=carVa.getWeigthNum(); //定義值車總載重數(shù)
double toMoney=carVa.getMoney(); //定義總價格?
int day=carVa.getDay();
int carType=0; //定義選擇的車序號
int maxC=999; //定義最大租車數(shù)量
for(int tf=3;tf>=0;tf--) {?
int confirmInt=input.nextInt(); //輸入數(shù)字判斷是否要租車
if(confirmInt==1) {
tf=-1; //結(jié)束外層租車需求循環(huán)判斷 18行
Car car[]={
new PassCar("奧迪A4",500,4),
new PassCar("馬自達6",400,4),
new PassCar("金龍",800,20),
new PickupCar("皮卡雪6",450,4,2),
new TruckCar("松花江",400,4),
new TruckCar("依維柯",1000,20),
};
System.out.println("序號"+"\t"+"汽車名稱"+"\t"+"租金"+"\t"+"容量");
for(int c=0,d=1;c<car.length;c++,d++) { //顯示租車列表
System.out.print(d+".");
car[c].show();
}
int carNum=3; //定義租車數(shù)量
System.out.println("請輸入您要租車的數(shù)量");
for(int tfNum=3;tfNum>=0;tfNum--) { //循環(huán)最大租車數(shù)量
carNum=input.nextInt();?
String carN[]=new String[carNum]; //定義數(shù)組,保存選擇后的車輛-車名
int carS[]=new int[carNum]; //定義數(shù)組,保存選擇后的車輛-座位數(shù)
double carW[]=new double[carNum]; //定義數(shù)組,保存選擇后的車輛-載重數(shù)
List<String> listS = new ArrayList<String>();?
List<String> listW = new ArrayList<String>();?
if(carNum>0&&carNum<=maxC) { //判斷租車數(shù)量.小于max=999, 進入選車循環(huán)
tfNum=-1;
for(int i=1,tfNo=3;i<=carNum;) {
System.out.println("請輸入第"+i+"輛車的序號,輸入"+"[ 0 ]"+"直接進入下一步租車天數(shù)界面");
carType=input.nextInt(); //選擇車序號
if(carType<=car.length&&carType>0) { //如果輸入的車序號小于車輛數(shù)據(jù)總長度
tfNo=3;
toMoney=toMoney+car[carType-1].getMoney(); //計算總價格?
toSeat=toSeat+car[carType-1].getSeatNum(); //計算總座位/載人數(shù)
toWeigth=toWeigth+car[carType-1].getWeigthNum(); //計算總載重數(shù)
carS[i-1]=car[carType-1].getSeatNum(); //保存我們選擇的車座位數(shù)為數(shù)組
carW[i-1]=car[carType-1].getWeigthNum();//保存我們選擇的車載重為數(shù)組
carN[i-1]=car[carType-1].getCarName(); //保存我們選擇的車名為數(shù)組
i++;
if(i<carNum) {
//顯示租車列表
System.out.println("序號"+"\t"+"汽車名稱"+"\t"+"租金"+"\t"+"容量");
for(int c=0,d=1;c<car.length;c++,d++) {?
System.out.print(d+".");
car[c].show();
}
}
}
else {
if(tfNo>0&&carType!=0) {
//顯示租車列表
System.out.println("序號"+"\t"+"汽車名稱"+"\t"+"租金"+"\t"+"容量");
for(int c=0,d=1;c<car.length;c++,d++) {?
System.out.print(d+".");
car[c].show();
}
System.out.println();
System.out.println("輸入錯誤!此序號的車輛不存在,還可輸入"+tfNo+"次。");
tfNo--;
}
else if(tfNo<=0) {
System.out.println("輸入的租車數(shù)量,錯誤次數(shù)已達3次,退出租車界面");
break;
}
}
//判斷車輛完選擇之后
if(i>carNum||carType==0) {?
System.out.println("請輸入租車天數(shù):");
day=input.nextInt();
if(day>0) { //判斷租車天數(shù)
System.out.println("您的賬單:");
System.out.println("***可載人的車有:");
for(int s=0;s<carN.length;s++) {
if(!listS.contains(carN[s])) { //判斷重復(fù)值
if(carS[s]>0) { //判斷座位數(shù)大于0
listS.add(carN[s]); //加到list
}
}
}
System.out.print(listS); //輸出去重復(fù)值后的車
if(toSeat<=0) { //判斷有沒有選擇客車
System.out.println("無");
}else {
System.out.println("\t總載人:"+toSeat+"人");
}
System.out.println("***可載貨的車有:");
for(int w=0;w<carN.length;w++) {
if(!listW.contains(carN[w])) { //判斷重復(fù)值
if(carW[w]>0) { //判斷座位數(shù)大于0
listW.add(carN[w]); //加到list
}
}
}
System.out.print(listW); //輸出 去重復(fù)值 后的車
if(toWeigth<=0) { //判斷有沒有選擇貨車
System.out.println("無");
}else {
System.out.println("\t總載貨:"+toWeigth+"噸");
}
System.out.println("租車總價格:"+toMoney*day+"元");
}else {
int tfDay=3;
System.out.println("租車天數(shù)錯誤,請重新輸入:");
while(tfDay>=0) {
day=input.nextInt();
tfDay--;
}
if(tfDay<=0) {
System.out.println("輸入租車天數(shù)錯誤次數(shù)已達最大值,默認給您選擇租車天數(shù)為1天");
day=1;
}
}
if(carType==0) {
break;
}
}
}
}
else{
if(tfNum>0) {
System.out.println("輸入錯誤!租車數(shù)量需要在1-"+maxC+"之間,還可輸入"+tfNum+"次。");
System.out.println("請重新輸入租車數(shù)量:");
}
else if(tfNum<=0) {
System.out.println("輸入的租車數(shù)量,錯誤次數(shù)已達3次,退出租車界面");
break;
}
}
}
}
else if(confirmInt==2) {
System.out.println("您選擇了否,感謝使用!");
break;
}
else if(confirmInt!=1||confirmInt!=2){
if(tf>0) {
System.out.println("輸入錯誤!還可輸入"+tf+"次。");
System.out.println("輸入數(shù)字選擇您是否要租車? 1是,2否");
}
else if(tf<=0) {
System.out.println("輸入需求錯誤次數(shù)已到達3次,退出租車界面。");
}
}
}
}
}
2020-07-21
歡迎參考我的學(xué)習(xí)筆記,包括第一季到第三季全部代碼~
https://www.jianshu.com/p/43f45dd0b22f
2020-07-12
真長長
2020-07-11
package java22;
import java.util.*;
import java.util.Scanner;?
public class RentCar {
public static void main(String[] args) {
System.out.println("歡迎使用答答租車系統(tǒng):");
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("請輸入您要租汽車的數(shù)量:");? ? ? ? ? ? ? ?
? ? ? ? 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("請輸入租車天數(shù):");//第4次輸入,租車天數(shù)
? ? ? ? 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("賬單計算結(jié)束.感謝您的光臨,祝您生活愉快!");
? ? ? ? }
? ? ? ? }
? ? ? ? }
? ? ? ? }
}else if(Rent.equals("0"))
? {
System.out.println("ByeBye!");
? }else{
? System.out.println("非法輸入!再見!");
? }? ? ? ?
}
}
}
我來粘個非對象程序的
2020-06-19
哇,真長