car类:
package com.CarRentSystem;
public class Vehicle {
String name;
int carryPerson;
int carryFreight;
int price;
public Vehicle(String name,int carryPerson,int carryFreight,int price){
this.name = name;
this.carryPerson = carryPerson;
this.carryFreight = carryFreight;
this.price = price;
}
public int rent(int days){
int rent = days * this.price;
return rent;
}
}主函数:
package com.CarRentSystem;
import java.util.Scanner;
public class Initial {
public static void main(String[] args){
Vehicle[] cars = {new Vehicle("奥迪A6",4,0,500),
new Vehicle("马自达6",4,0,400),
new Vehicle("皮卡雪6",4,2,450),
new Vehicle("金龙",20,0,800),
new Vehicle("松花江",0,4,400),
new Vehicle("依维柯",0,20,1000)};
Scanner input = new Scanner(System.in);
System.out.println("欢迎使用喵喵租车系统!");
System.out.println("请问是否需要租车:1是,0否");
int ifRent = Integer.parseInt(input.next());
if ( ifRent == 1){
System.out.println("您可选择的租车类型及价目表:");
System.out.println("序号\t汽车名称\t\t租金\t\t\t容量");
for (int i = 0 ;i < 6;i++){
System.out.print(i + 1 + "\t");
System.out.print(cars[i].name + "\t\t" );
System.out.print(cars[i].price + "元/天\t\t");
if (cars[i].carryPerson == 0){
System.out.println("载货:" + cars[i].carryFreight + "吨");
}else if (cars[i].carryFreight == 0){
System.out.println("载人:" + cars[i].carryPerson + "人");
}else{
System.out.println("载人:" + cars[i].carryPerson + "人,载货:" + cars[i].carryFreight + "吨");
}
}
}else{
System.out.println("欢迎再次使用本系统!");
return ;
}
System.out.println("请输入需要租车的总天数:");
int days = Integer.parseInt(input.next());
System.out.println("请输入需要租车的总数量:");
int carCount = Integer.parseInt(input.next());
int allCars[] = new int[carCount];
for (int j = 1;j < carCount + 1;j++){
System.out.println("选择第" + j + "辆车的编号:");
allCars[j-1] = Integer.parseInt(input.next());
}
System.out.print("您选择的车辆为:");
int allPerson = 0;
int allFreight = 0;
int allPrice = 0;
for (int m = 0; m < carCount; m++){
int carCode = allCars[m];
System.out.print(cars[carCode].name + " ");
allPerson += cars[carCode].carryPerson;
allFreight += cars[carCode].carryFreight;
allPrice += cars[carCode].rent(days);
}
System.out.println();
System.out.println("共可载人:" + allPerson + "人");
System.out.println("共可载货:" + allFreight + "吨");
System.out.println("总价格为:" + allPrice + "元");
}
}输出如下:
欢迎使用喵喵租车系统!
请问是否需要租车:1是,0否
1
您可选择的租车类型及价目表:
序号 汽车名称 租金 容量
1 奥迪A6 500元/天 载人:4人
2 马自达6 400元/天 载人:4人
3 皮卡雪6 450元/天 载人:4人,载货:2吨
4 金龙 800元/天 载人:20人
5 松花江 400元/天 载货:4吨
6 依维柯 1000元/天 载货:20吨
请输入需要租车的总天数:
2
请输入需要租车的总数量:
2
选择第1辆车的编号:
2
选择第2辆车的编号:
3
您选择的车辆为:皮卡雪6 金龙
共可载人:24人
共可载货:2吨
总价格为:2500元
Process finished with exit code 0
你没发现输出结果与序号不匹配吗?车辆序号都往后移了一位。
从49行数组后面有点懵