刚刚看完java入门第二季的视频,尝试着写了一下答答租车系统,但是运行时出错,各位大神帮帮忙看一下吧。(最后求和和输出还差点,但是应该不影响运行,只是输出的不一样),以下是我的代码: //车的类我贴上了货车的,其他的几个类都差不多。 主类: package Test; import java.util.*; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub int sum = 0; Car[] cars = { new jiaoche(1,"奥迪A4",500,4), new jiaoche(2,"马自达6",400,4), new all(3,"皮卡雪6",450,4,2), new jiaoche(4,"金龙 ",800,20), new huoche(5,"松花江",400,4), new huoche(6,"依维柯",1000,20) }; System.out.println("是否进入租车系统(1:是 2:否)"); Scanner reader = new Scanner(System.in); if(reader.nextInt() == 2){ System.out.println("再见"); System.exit(0); } reader.close(); System.out.println("可供您选择的车辆:"); System.out.println("序号 车名 价格 载重"); ((jiaoche)cars[0]).show(); ((jiaoche)cars[1]).show(); ((all)cars[2]).show(); ((jiaoche)cars[3]).show(); ((huoche)cars[4]).show(); ((huoche)cars[5]).show(); System.out.println("请输入您需要的车辆数:"); Scanner num = new Scanner(System.in); for(int i = 0;i < num.nextInt();i++){ System.out.println("请输入第"+i+"辆车序号:"); Scanner x = new Scanner(System.in); sum += cars[x.nextInt()].getCoin(); x.close(); } num.close(); System.out.println("*****您共需要支付"+sum+"元"); } }
货车类:
package Test;
public class huoche extends Car{
private int zh;
public int getZh() {
return zh;
}
public void setZh(int zh) {
this.zh = zh;
}
public void show(){
System.out.println(getId()+" "+getName()+" "+getCoin()+
"元/天 载物(吨):"+getZh());
}
public huoche(int id,String name,int coin,int zh) {
// TODO Auto-generated method stub
this.setId(id);
this.setName(name);
this.setCoin(coin);
this.setZh(zh);
}
}