嗯,发个代码供大家参考。
package common; import java.util.Scanner; public class SentCar { public static void main(String[] args) { int price = 0; int totalPeople = 0; int totalShop = 0; Car[] cars = new Car[3]; cars[0] = new CarryPeople("擎天柱", 4, 300, 1); cars[1] = new CarryShop("大黄蜂", 20, 400, 2); cars[2] = new CarryAll("红蜘蛛", 4, 40, 800, 3); System.out.println("欢迎使用变形金刚租车系统"); System.out.println("您是否要租车:1是 0否"); Scanner scan = new Scanner(System.in); String str1 = scan.next(); if(str1.equals("0")){ System.out.println("欢迎下次光临!再见"); }else if(str1.equals("1")){ System.out.println("您可使用的租车类型和价格如下:"); for (int i = 0;i < 3;i++){ cars[i].printMessage(); } System.out.println("请输入你要租车的数量:"); //Scanner input = new Scanner(System.in); //scanner语句开辟的内存只有一个 int number = scan.nextInt(); String[] name = new String[number]; Car[] selectCars = new Car[number]; for(int i = 0;i < number;i++){ System.out.println("请输入你要租的第"+ (i+1) +"车的类型"); int type = scan.nextInt(); if(type > 3| type < 0){ System.out.println("输入不合法!"); }else{ System.out.println("第" + (i+1) +"辆车是" + cars[type - 1].name); price = price + cars[type - 1].price; name[i] = cars[type - 1].name; selectCars[i] = cars[type -1]; } } System.out.println("请选择你需要的租车天数:"); int day = scan.nextInt(); scan.close(); price = price * day; System.out.println("您的账单是:"); System.out.print("变形金刚系列中可载人的车有:" + " "); for(int i = 0;i < number; i ++){ if(selectCars[i].busload != 0){ System.out.print(selectCars[i].name + " "); totalPeople = totalPeople + selectCars[i].busload; } } System.out.println("总载人量是:" + totalPeople + "人"); System.out.print("变形金刚系列中可载货的车有:" + " "); for(int i = 0;i < number; i ++){ if(selectCars[i].cargo != 0){ System.out.print(selectCars[i].name + " "); totalShop = totalShop + selectCars[i].cargo; } } System.out.println("总载货量是:" + totalShop + "吨"); System.out.println("您的租车总价格是"+ price +"元。"); } } }