610353537
2015-01-14 10:59
在Test里可载人的地方出现空指针异常运行不下去,求指点,怎么改呢
package com.caosheng.TEST1;
/*
* 所有汽车的父类
*
*/
public class Car {
private int carNum;//定义汽车的序号
private String carName;//定义汽车的名字
private int carRentMoney;//定义汽车的租金
public int getCarNum() {
return carNum;
}
public void setCarNum(int carNum) {
this.carNum = carNum;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public int getCarRentMoney() {
return carRentMoney;
}
public void setCarRentMoney(int carRentMoney) {
this.carRentMoney = carRentMoney;
}
}
package com.caosheng.TEST1;
/*
* 定义载客的客车
*
*/
public class ZaikeCar extends Car {
private int zaikeNum;//定义载客客车的载客量
public ZaikeCar(int carNum,String carName,int carRentMoney,int zaikeNum){
this.setCarName(carName);
this.setCarNum(carNum);
this.setCarRentMoney(carRentMoney);
this.zaikeNum=zaikeNum;
}
public int getZaikeNum() {
return zaikeNum;
}
public void setZaikeNum(int zaikeNum) {
this.zaikeNum = zaikeNum;
}
}
package com.caosheng.TEST1;
/*
* 定义一个可以载货也可以载货的皮卡
*/
public class PikaCar extends Car {
private int zaikeNum;//定义皮卡的载客量
private int zaihuoNum;//定义皮卡的载货量
public PikaCar(int carNum,String carName,int carRentMoney,int zaikeNum,int zaihuoNum){
this.setCarNum(carNum);
this.setCarName(carName);
this.setCarRentMoney(carRentMoney);
this.zaikeNum=zaikeNum;
this.zaihuoNum=zaihuoNum;
}
public int getZaikeNum() {
return zaikeNum;
}
public void setZaikeNum(int zaikeNum) {
this.zaikeNum = zaikeNum;
}
public int getZaihuoNum() {
return zaihuoNum;
}
public void setZaihuoNum(int zaihuoNum) {
this.zaihuoNum = zaihuoNum;
}
}
package com.caosheng.TEST1;
/*
* 构造用于载货的货车
*/
public class ZaihuoCar extends Car {
private int zaihuoNum;//定义货车的载货量
public ZaihuoCar(int carNum,String carName,int carRentMoney,int zaihuoNum){
this.setCarNum(carNum);
this.setCarName(carName);
this.setCarRentMoney(carRentMoney);
this.zaihuoNum=zaihuoNum;
}
public int getZaihuoNum() {
return zaihuoNum;
}
public void setZaihuoNum(int zaihuoNum) {
this.zaihuoNum = zaihuoNum;
}
}
package com.caosheng.TEST1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用答答租车系统!");
System.out.println("您是否要租车?:1是 0否");
Scanner c = new Scanner(System.in);
int k=c.nextInt();
if(k==1){
System.out.println("您可租车的类型及价格表");
Car [ ] carForRent={new ZaikeCar(1,"奥迪A4",500,4),new ZaikeCar(2,"马自达6",400,4),
new PikaCar(3,"皮卡雪6",450,4,2),new ZaikeCar(4," 金龙 ",800,20),
new ZaihuoCar(5,"松花江",400,4),new ZaihuoCar(6,"依维柯",1000,20)};
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"\t"+"容量");
for (Car car : carForRent){
if(car instanceof ZaikeCar){
System.out.println(car.getCarNum()+"\t"+car.getCarName()+"\t"+car.getCarRentMoney()
+"/天"+"\t"+"载人:"+((ZaikeCar)car).getZaikeNum()+"人");
}
if(car instanceof PikaCar){
System.out.println(car.getCarNum()+"\t"+car.getCarName()+"\t"+car.getCarRentMoney()
+"/天"+"\t"+
"载人:"+((PikaCar)car).getZaikeNum()+"人"+"\t"+
"载货:"+((PikaCar)car).getZaihuoNum()+"吨");
}
if(car instanceof ZaihuoCar){
System.out.println(car.getCarNum()+"\t"+car.getCarName()+"\t"+car.getCarRentMoney()
+"/天"+"\t"+"\t"+"载货:"+((ZaihuoCar)car).getZaihuoNum()+"吨");
}
}
System.out.println("请输入您要租车的数量:");
int carNum2=c.nextInt(); //用于接收汽车的总数量
int sumR=0;
int sumM=0;
int [] nameN=new int[carNum2];
for(int i=1;i<=carNum2;i++){
System.out.println("请输入第"+i+"辆车的序号:");
int xuhao=c.nextInt();
nameN[i-1]=xuhao;
if(xuhao==1||xuhao==2||xuhao==4){
sumR+=((ZaikeCar)carForRent[i-1]).getZaikeNum();
}else if(xuhao==3){
sumR+=((PikaCar)carForRent[i-1]).getZaikeNum();
sumM+=((PikaCar)carForRent[i-1]).getZaihuoNum();
}else if(xuhao==5||xuhao==6){
sumM+=((ZaihuoCar)carForRent[i-1]).getZaihuoNum();
}else{
System.out.println("您输入的序号有错误");
}
}
//System.out.println(sumR);
System.out.println("请输入您要租车的天数:");
int day=c.nextInt();
System.out.println("您的账单:");
System.out.println("**可载人的车有:");
for(int a:nameN){
System.out.println(a);
}
for(int ii=1;ii<=nameN.length;ii++){
int n=nameN[ii];
if(ii==carForRent[n].getCarNum()){
System.out.print(carForRent[n].getCarName()+"\t");
}
}
System.out.println("共载人"+sumR);
System.out.println("**可载货的车有:");
for(int jj=1;jj<=nameN.length;jj++){
if(jj==carForRent[nameN[jj]].getCarNum()){
System.out.print(carForRent[nameN[jj]].getCarName()+"\t");
}
}
System.out.print("共载货"+sumM);
}else if(k==0){
System.out.println("感谢您的使用再见!");
}else{
System.out.println("您输入的信息有错!");
}
}
}
你这个程序Bug不止一个啊....
我也想问为什么???
我知道输入汽车的数量和carForRent[].length不一样多,没想出来怎么改正,求指点
Java入门第二季 升级版
530650 学习 · 6091 问题
相似问题