慕粉1471134825
2017-04-14 17:41
for( k=0;k<total;k++){
System.out.println("请输入第"+""+(k+1)+"辆车的序号");
chooseNums[k]=a.nextInt();
}
System.out.println("请输入租车天数");
int days =a.nextInt();
int totalMoney=0;
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
int money1=0;
int money2=0;
int money3=0;
int money=0;
int j;
for(j=0;j<total;j++){
if(messenge[chooseNums[k]] instanceof Car){
System.out.println(messenge[chooseNums[k]].name);
money1+=messenge[chooseNums[k]].rent;
}else if(messenge[chooseNums[k]] instanceof PickUp){
System.out.println(messenge[chooseNums[k]].name);
money2+=messenge[chooseNums[k]].rent;
}
提示数组过界
package project6;
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 a = new Scanner(System.in);
AutoMobile[] messenge={new Car(1,"奥迪A4",500,4),new Car(2,"马自达6",400,4),
new PickUp(3,"皮卡雪",450,4,2),new Car(4,"金龙",800,20),
new Trunk(5,"松花江",400,4),new Trunk(6,"依维柯",1000,20)};
int i =a.nextInt();
if(i==1){
System.out.println("您可租车的类型及其价目表");
System.out.println("序号\t汽车名称\t租金\t容量");
for(int j=0;j<6;j++){
if(messenge[j] instanceof Car){
System.out.println(messenge[j].num+"\t"+messenge[j].name+"\t"+messenge[j].rent+"元/天"+"\t"+"载人:"+((project6.Car) messenge[j]).getPeopleNums()+"人");
}
if(messenge[j] instanceof Trunk){
System.out.println(messenge[j].num+"\t"+messenge[j].name+"\t"+messenge[j].rent+"元/天"+"载货:"+((Trunk) messenge[j]).getThingNums()+"吨");
}
if(messenge[j] instanceof PickUp){
System.out.println(messenge[j].num+"\t"+messenge[j].name+"\t"+messenge[j].rent+"元/天"+"载人:"+((PickUp) messenge[j]).getPeopleNums()+"载货:"+((PickUp) messenge[j]).getThingNums()+"吨");
}
}
}
System.out.println("请输入您要租汽车的数量");
int total =a.nextInt();
int[] chooseNums = new int [total];
int k;
for( k=0;k<total;k++){
System.out.println("请输入第"+""+(k+1)+"辆车的序号");
chooseNums[k]=a.nextInt();
}
System.out.println("请输入租车天数");
int days =a.nextInt();
int totalMoney=0;
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
int money1=0;
int money2=0;
int money3=0;
int money=0;
int j;
for(j=0;j<total;j++){
if(messenge[chooseNums[k]] instanceof Car){
System.out.println(messenge[chooseNums[k]].name);
money1+=messenge[chooseNums[k]].rent;
}else if(messenge[chooseNums[k]] instanceof PickUp){
System.out.println(messenge[chooseNums[k]].name);
money2+=messenge[chooseNums[k]].rent;
}
}
System.out.println("***载货的车有:");
// for(j=0;j<total;j++){
if(messenge[chooseNums[k]] instanceof Trunk){
System.out.println(messenge[chooseNums[k]].name);
money3+=messenge[chooseNums[k]].rent;
}
else if(messenge[chooseNums[k]] instanceof PickUp){
System.out.println(messenge[chooseNums[k]].name);
}
//}
money=money1+money2+money3;
totalMoney=money*days;
System.out.println("租车的总价格:"+""+totalMoney+"元");
}
}
代码没贴全啊,你贴出来的这个代码片段没法判断各个数组的长度,另外,既然你知道了是数组越界,控制台应该还会提示行数吧, 根据行数就能很容易定位错误在哪
Java入门第二季 升级版
530670 学习 · 6091 问题
相似问题