为什么综合练习的名字始终是null呢?

来源:12-2 项目问题解析 1

Smile53

2016-03-17 18:09

package com.imooc.test7;


import java.util.Scanner;


public class Test {


public static void main(String[] args) {

// TODO Auto-generated method stub

//创建车辆信息

Car[] car = {new PessagerCar("奥迪A4 ", 500, 4),new CargoCar("松花江", 400, 4),new PukiCar("皮卡雪6", 450, 4,2)};

System.out.println("欢迎来到租车地带!");

System.out.println("请问是否需要租车:1是 2否");

//显示租车信息

Scanner in = new Scanner(System.in);

int choice = in.nextInt();

while(choice != 2 || choice !=1){

//如果输入错误,非0/1,则重新输入

if(choice == 1){

System.out.println("你可租车的类型及价目表:");

System.out.println(""+"序号" + "\t" + "汽车名称" + "\t" + "租金" + "\t" + "容量");

int i =0;

for(Car curCar:car){ //定义一个curCar对象遍历car数组

i++;

if(curCar instanceof PessagerCar){

System.out.println("" + i + "\t" + curCar.getName() + "\t"  + curCar.getRent() + "元/辆"+ "\t"+ ((PessagerCar) curCar).getManned() + "人");

//car[i].show();  //显示租车信息及序号

}

if(curCar instanceof CargoCar){

System.out.println("" + i + "\t" + curCar.getName() + "\t" + curCar.getRent() + "元/辆" +"\t"+ ((CargoCar) curCar).getCargo() + "吨");

}

if(curCar instanceof PukiCar){

System.out.println("" + i + "\t" + curCar.getName() + "\t" + curCar.getRent() + "元/辆" +"\t"+ ((PukiCar) curCar).getManned() + "人" + ((PukiCar) curCar).getCargo() + "吨");

}

}

break;

}else{

System.out.println("谢谢你的光临,再见!");

}

}

//租车数量

System.out.println("请输入你需要租车的数量:");

int carnum = in.nextInt();

Car[] choicecar = new Car[carnum]; //将选择的序号存入数组

for(int i = 0;i < carnum ;i++){

System.out.println("请输入第"+ (i+1) +"辆车的序号");

int num = in.nextInt();

choicecar[i] = car[num-1];

}

//租车天数

System.out.println("请输入租车天数");

int rentday = in.nextInt();

//计算并显示账单

System.out.println("*********************您的账单:**********************");

int allmoney = 0;//总的租金

System.out.println("您总共要租借"+rentday+"天");

//计算总载客数载货量

int allPesCar = 0;  //总载人数

int allCargo = 0;   //总载货量

for(int i=0; i<choicecar.length ; i++){

if(choicecar[i] instanceof PessagerCar){

allPesCar += ((PessagerCar) choicecar[i]).getManned();

allmoney += ((PessagerCar) choicecar[i]).getRent();

}

if(choicecar[i] instanceof CargoCar){

allCargo += ((CargoCar) choicecar[i]).getCargo();

allmoney += ((CargoCar) choicecar[i]).getRent();

}

if(choicecar[i] instanceof PukiCar){

allPesCar += ((PukiCar) choicecar[i]).getManned();

allCargo +=((PukiCar)choicecar[i]).getCargo();

allmoney += ((PessagerCar) choicecar[i]).getRent();

allmoney += ((CargoCar) choicecar[i]).getRent();

}

}

//输出清单

System.out.println("您所要租借的总载客量为: " + allPesCar + "人\t" + "总载货量为:" + allCargo + "吨");

        int totalPrice = allmoney*rentday;   //总价

        System.out.println("您总共需要支付:  " + totalPrice  + "  元");

        System.out.println("谢谢光临租车地带,下次再见!");

}

}


写回答 关注

2回答

  • Smile53
    2016-03-19 15:49:40
    http://img.mukewang.com/56ed0484000118b505520368.jpg并没有错误信息
  • xust
    2016-03-18 10:18:50

    可以把报错信息贴上来么?

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530559 学习 · 6091 问题

查看课程

相似问题