菜鸟从第一季看到这里 研究了半天 我也来交个作业
我只写了两个class 一个data数据class 一个入口class 通过逻辑实现调用赋值 并组合输出 代码如下 希望老师指教:
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 + " 辆车的序号,结束选车请输入0:");
System.out.println("当前可租的车辆有:\n 1.奥迪A4 2.马自达6 3.皮卡雪6 4.金龙 5.松花江 6.依维柯");
String num = input.next();
if (i == 10) {
System.out.println("频繁输入错误 系统结束!");
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("结束选车!");
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("请输入租用天数,停止租车请输入0:");
String num2 = input.next();
if (i == 10) {
System.out.println("频繁输入错误 系统结束!");
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("租车结束,您停止了租车!");
}
}
}
设计了一定的交互式 我发现自己编写过程中总有新的想法冒出来 于是改了又改 耽误了很久才最终完成 测试过没有漏洞 可能还有我不知道的漏洞和优化空间
交作业 以上!