【问题标题】:how to put value in array that is also in array or to assign value to an array from another array?如何将值放入同样位于数组中的数组中或将值分配给另一个数组中的数组?
【发布时间】: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


【解决方案1】:

如果你想将二维数组从一个分配到另一个。

按照下面的代码sn-p,

String[][] newArray = new String[roomType[0].length][roomType[1].length];

        for (int i = 0; i < roomType[0].length; i++){
            for (int j = 0; j < roomType[1].length; j++){
                newArray[i][j] = roomType[i][j];
            }
        }

如果要交叉检查试试这个,数组是否正确分配,试试下面的,

for (int i = 0; i < newArray[0].length; i++){
            for (int j = 0; j < newArray[1].length; j++){
                System.out.print(newArray[i][j] + " ");
            }
            System.out.println("");
        }

【讨论】:

  • 我给了你代码的一种方式。如果你愿意,你可以使用它
  • 是的!先生,我可以用它。
  • @Vig Nesh 先生?你在吗?
  • 他说他已经给了你答案中需要的代码。
【解决方案2】:

试试这个,

import java.io.*;

public class ArrayTest {
public static void main(String args[]) throws NumberFormatException,
        IOException {
    double GuestRate = 100;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    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
     -----------------------------------------------------------------*/
    // 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
    int available;

    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;

        int row = SwitchOne - 1;
        int roomID = Integer.parseInt(roomType[row][3]);
        if (two == roomID) {

            double price = Double.parseDouble(roomType[row][1]);

            available = Integer.parseInt(roomType[row][2]);

            if( Integer.parseInt(roomType[row][2]) >= Integer.parseInt(customer[x][3]))
            {
                available = available-Integer.parseInt(customer[x][3]);
            String avail = Integer.toString(available);
            roomType[row][2] = avail;
            customer[x][5] = roomType[row][0];
            double night = Double.parseDouble(customer[x][4]);
            double guest = Double.parseDouble(customer[x][3]);

            guest *= GuestRate * night;

            price *= night;
            double totalAmount = guest + price;
            String AmountTotal = Double.toString(totalAmount);

            customer[x][6] = AmountTotal;
            System.out.println(customer[x][6]);
            }
            else
                System.out.println("Room is not available for "+ customer[x][3]+ " Guests : We have only "+available+  " vacancy in that room");

        }

    }
}

}

它的工作..输出是,

输入记录数:3

确认编号:1

名字:f 姓氏:g 客人:2 晚上:2

  1. 标准.................................................. P500.00
  2. 双..................................... ..P800.00
  3. 婚姻..................................................P3,500.00
  4. 三重................................................ ..P4,500.00

请选择房型:1 5 1400.0

确认编号:2

名字:e 姓氏:r 客人:2 晚上:2

  1. 标准.................................................. P500.00
  2. 双..................................... ..P800.00
  3. 婚姻..................................................P3,500.00
  4. 三重................................................ ..P4,500.00

请选择房型:1 3 1400.0

确认号:3

名字:d 姓氏:g 客人:2 晚上:2

  1. 标准.................................................. P500.00
  2. 双..................................... ..P800.00
  3. 婚姻..................................................P3,500.00
  4. 三重................................................ ..P4,500.00

请选择房型:1 1 房间不能容纳 2 位客人:我们只有 1 个空位在那个房间里

【讨论】:

  • 是的,但如果我把 2 条记录放在同一个房间里,编译器会说它的 OutOfBound
  • 好的,所以我注册了 1,所以这意味着 1 条记录,它运行完美,但是当我注册 2 = 2 条记录并将它们放在同一个房间时,它会出错。
  • Exception in thread "main" java.lang.NumberFormatException: For input string: "2.0" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at sample.BB.main(BB.java:250)
  • 我已经编辑了那个代码.. 请试试.. 抱歉耽搁了... :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多