【发布时间】:2015-12-29 12:37:01
【问题描述】:
这是我想放入另一个数组的array。
String [][]roomType = new String[4][4];
roomType[0][0] = "Standard";
roomType[0][1] = "500";
roomType[0][2] = "5";
roomType[0][3] = "1";
roomType[1][0] = "Double";
roomType[1][1] = "800";
roomType[1][2] = "4";
roomType[1][3] = "2";
roomType[2][0] = "Matrimonial";
roomType[2][1] = "3500";
roomType[2][2] = "6";
roomType[2][3] = "3";
roomType[3][0] = "Triple";
roomType[3][1] = "4500";
roomType[3][2] = "5";
roomType[3][3] = "4";
/*-----------------------------------------------------------------
* Costumer's Information
-----------------------------------------------------------------*/
do{//Start Of First Loop - Customer's Info
System.out.print("\nEnter Number Of Records : ");
int Records = Integer.parseInt(br.readLine());
String [][] customer = new String [Records][7]; //records for customer
for (int x = 0; x < Records; x++){ // Start For Records
System.out.print("\nConfimation Number: ");
customer[x][0] = br.readLine();
System.out.print("\nFirst Name: ");
customer[x][1] = br.readLine();
System.out.print("Last Name: ");
customer[x][2] = br.readLine();
System.out.print("Guest: ");
customer[x][3] = br.readLine();
System.out.print("Night: ");
customer[x][4] = br.readLine();
System.out.println();
System.out.print("1. Standard..............................................P500.00\n");
System.out.print("2. Double................................................P800.00\n");
System.out.print("3. Matrimonial...........................................P3,500.00\n");
System.out.print("4. Triple................................................P4,500.00 \n");
System.out.print("\n\nPlease Select Room Type: ");
int SwitchOne = Integer.parseInt(br.readLine());
int two = SwitchOne;
switch(SwitchOne){
case 1:
case 2:
case 3:
case 4:
这是将数组放入另一个数组或从数组中为数组赋值的过程。
for(int row=SwitchOne-1;row<4;row++){
int roomID = Integer.parseInt(roomType[row][3]);
if(two == roomID){
double price = Double.parseDouble(roomType[row][1]);
int available = Integer.parseInt(roomType[row][2]);
available -= 1;
String avail = Double.toString(available);
roomType[row][2] = avail;
customer[x][5] = roomType[row][0];
double guest = Double.parseDouble(customer[x][3]);
guest *= GuestRate;
double night = Double.parseDouble(customer[x][4]);
price *= night;
double totalAmount = guest + price;
String AmountTotal = Double.toString(totalAmount);
customer[x][6] = AmountTotal;
}
}
问题是当第一个放置的数组不能再次使用时。编译器说它的OutOfBounds。
所以我无法选择之前选择的内容。
我是Java班的新手,请帮帮我。
这只是 1 个方法类。 我无法理解 2 种或更多方法。
不明白就评论吧,太难解释了。
【问题讨论】:
-
你真的不应该在这种情况下使用(多维)数组。为 Customer 和 Room 创建类,如果需要,可以将这些对象存储在数组中。
-
不是数组越界,而是索引。您在方括号 [n] 中使用了太大的数字。
-
我不能使用更多的类 @HannoBinder 先生,我不知道怎么做,教授只说 1 种方法。
-
#Geoddie 你能提到你的编译器抛出的错误吗?
标签: java arrays loops indexoutofboundsexception