1

学校の課題に取り組んでいますが、2D配列に問題があります。基本的に、私のプログラムはユーザーから整数を受け取り、配列値の1つがその整数よりも小さいかどうかを判断します。トリックを実行するためにしばらくの間forループを設定していますが、データの最後の行を使用するようにループを取得できないようです。私は今、数時間物事を混ぜ合わせてみました。

足りないものは本当にひどくダーピングしていると思いますが、今は手を使うことができます。これが私のコードに関連するデータです

    //array of store inventory
    int invArray[][] = {{102, 54, 20, 78}, 
                        {45, 25, 35, 75},
                        {12, 35, 45, 65},
                        {54, 25, 34, 45},
                        {15, 35, 50, 25}};
    int menuChoice = 0;                     //allows user to select a menu option
    int storeNum=0;                         //store number
    String itemName="null";                 //name for an item
    int itemAmount=0;                       //items below this amount are pulled up in choice 4
    int arrayRow=0;                         //row value for invArray based on itemName
    int arrayCol=0;                         //column value for invArray based on storeNum


       //menu 4
        if (menuChoice == 4){
            System.out.print("Find all items below the number: ");

            //catches mistakes
            try{
            itemAmount = input.nextInt();
            }
            catch(Exception e){
                System.out.print("Invalid value entered. Try again: ");
                itemAmount = input.nextInt();
            }
            System.out.println("Items below "+itemAmount+": ");

            //loop that checks item values at all stores
            arrayRow=0; 
            arrayCol=0;                         //counters for loop
            while (arrayCol < invArray[arrayRow].length){

                for (arrayRow = 0; arrayRow < invArray[arrayCol].length; arrayRow++){


                    if (invArray[arrayRow][arrayCol] < itemAmount){


                        if (arrayRow == 0)
                            itemName = "Tennis Shoes";
                        if (arrayRow == 1)
                            itemName = "Sweaters";
                        if (arrayRow == 2)
                            itemName = "Jeans";
                        if (arrayRow == 3)
                            itemName = "Shorts";
                        if (arrayRow == 4)
                            itemName = "Jackets";


                        //outputs stores with values lower than the one given
                        System.out.println("Store 10"+(arrayCol+1)+" "+itemName+": "
                        +invArray[arrayRow][arrayCol]);

                        for (int i = 0;invArray[4][i] < invArray[arrayRow].length; i++)
                            System.out.println("Store 10"+(i+1)+" "+" jackets : "+invArray[i][4]);
                    }


            }       
                arrayCol++;
                System.out.println();
        }//end item check loop
    }
4

3 に答える 3

3

2番目のループが間違っています。そのはず

for (arrayRow = 0; arrayRow < invArray.length; arrayRow++){

arrayRow最初のインデックスです。コードでは、5ではなく4を取得するため、エラーが発生します。

またfor、両方のループに使用することをお勧めします。

また、前者は依存しているため、行ループ内に列ループを配置するか、インデックスを交換する必要があります。

于 2012-12-09T23:54:16.830 に答える
1

これを試して:

while (arrayRow < invArray.length) {
    for (arrayCol = 0; arrayCol < invArray[arrayRow].length; arrayCol++) {
        //...
    }
    arrayRow++;
}

配列には次元が[4][5]あります; 2つを切り替えて、配列に次元があるかのように要素にアクセスしようとしています[5][4]。また、すべての作業invArray[a].lengthで常に4になるため、16個の要素にのみアクセスします。a

于 2012-12-10T00:03:11.817 に答える
0

2行目が必要ですか

itemName = "Jackets";
arrayCol++;

あなたのコードをバールすると私は少し混乱するので、これが解決するかどうかはわかりません。

于 2012-12-09T23:44:30.103 に答える