菜鳥從第一季看到這里 研究了半天 我也來交個作業(yè)
我只寫了兩個class 一個data數(shù)據(jù)class? 一個入口class 通過邏輯實現(xiàn)調(diào)用賦值 并組合輸出 代碼如下 希望老師指教:
CarAllData類
public class CarAllData {
public String carName;
public int carRent;
public int carManVolume = 0;
public int carGoodsVolume = 0;
public void checkCar(int checkNum) {
if (checkNum == 1) {
this.carName = "奧迪A4";
this.carRent = 500;
this.carManVolume = 4;
}
if (checkNum == 2) {
this.carName = "馬自達6";
this.carRent = 400;
this.carManVolume = 4;
}
if (checkNum == 3) {
this.carName = "皮卡雪6";
this.carRent = 450;
this.carManVolume = 4;
this.carGoodsVolume = 2;
}
if (checkNum == 4) {
this.carName = "金龍";
this.carRent = 800;
this.carManVolume = 20;
}
if (checkNum == 5) {
this.carName = "松花江";
this.carRent = 400;
this.carGoodsVolume = 4;
}
if (checkNum == 6) {
this.carName = "依維柯";
this.carRent = 1000;
this.carGoodsVolume = 20;
}
}
}
main類
import java.util.Scanner;
public class mainCar {
public static void main(String[] args) {
int rightNum = 0;
int rightRentDay = 0;
String[] carNameArray = new String[6];
int carRentCount = 0;
int carManVolumeCount = 0;
int carGoodsVolumeCount = 0;
int scannerCount = 0;
int[] scannerNum = { 0, 0, 0, 0, 0, 0, 0 };
Scanner input = new Scanner(System.in);
for (int j = 1; (j <= carNameArray.length && rightNum != -1); j++) {
CarAllData checkCar = new CarAllData();
for (int i = 0; i <= 10; i++) {
System.out.println("請輸入您要租用的第 " + j + " 輛車的序號,結(jié)束選車請輸入0:");
System.out.println("當前可租的車輛有:\n 1.奧迪A4? 2.馬自達6? 3.皮卡雪6? 4.金龍? ?5.松花江? ?6.依維柯");
String num = input.next();
if (i == 10) {
System.out.println("頻繁輸入錯誤 系統(tǒng)結(jié)束!");
rightNum = -1;
break;
}
try {
rightNum = Integer.parseInt(num);
} catch (Exception e) {
System.out.println("輸入有誤 請重新輸入!");
continue;
}
if (rightNum < 0 || rightNum > 6) {
System.out.println("輸入有誤 請重新輸入!");
continue;
}
if (rightNum != 0) {
for (int checkNum : scannerNum) {
if (checkNum == rightNum) {
System.out.println("車輛選擇重復 請重新輸入!");
rightNum = -2;
break;
}
}
if (rightNum == -2) {
continue;
}
}
break;
}
if (rightNum != 0 && rightNum != -1) {
checkCar.checkCar(rightNum);
carNameArray[j - 1] = checkCar.carName;
System.out.println("第 " + j + " 輛車選擇成功,車輛為:" + checkCar.carName);
scannerCount++;
} else if (rightNum != -1) {
System.out.println("結(jié)束選車!");
break;
}
carRentCount += checkCar.carRent;
carManVolumeCount += checkCar.carManVolume;
carGoodsVolumeCount += checkCar.carGoodsVolume;
scannerNum[j] = rightNum;
}
for (int i = 0; (scannerCount > 0 && i < 100 && rightNum != -1); i++) {
System.out.println("請輸入租用天數(shù),停止租車請輸入0:");
String num2 = input.next();
if (i == 10) {
System.out.println("頻繁輸入錯誤 系統(tǒng)結(jié)束!");
rightNum = -1;
break;
}
try {
rightRentDay = Integer.parseInt(num2);
} catch (Exception e) {
System.out.println("租期輸入有誤 請重新輸入!");
continue;
}
if (rightRentDay < 0 || rightRentDay > 100) {
System.out.println("租期輸入有誤 請重新輸入!");
continue;
}
if (rightRentDay == 0) {
scannerCount = 0;
}
break;
}
if (scannerCount > 0 && rightNum != -1) {
System.out.print("租車成功!\n··您選擇的車輛為:");
for (int s = 0; s < scannerCount; s++) {
System.out.print(carNameArray[s] + "? ");
}
System.out.println();
if (carManVolumeCount != 0) {
System.out.println("··合計可載人:" + carManVolumeCount + "人 ");
}
if (carGoodsVolumeCount != 0) {
System.out.println("··合計可載貨:" + carGoodsVolumeCount + "噸 ");
}
System.out.println("··租期為:" + rightRentDay + "天? ?合計租金為:" + carRentCount * rightRentDay + "元");
}
if (scannerCount == 0 && rightNum != -1) {
System.out.println("租車結(jié)束,您停止了租車!");
}
}
}
設(shè)計了一定的交互式? 我發(fā)現(xiàn)自己編寫過程中總有新的想法冒出來 于是改了又改? 耽誤了很久才最終完成 測試過沒有漏洞 可能還有我不知道的漏洞和優(yōu)化空間?
交作業(yè) 以上!
2020-10-06
兄弟? 你這代碼這么長? 是個狠人