Java 二维数组 for 循环不读取最后一个值

标签 java multidimensional-array

我正在做一项学校作业,我遇到了二维数组的问题。基本上,我的程序从用户那里获取一个整数,并确定其中一个数组值是否小于该整数。我有一个 while 和 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
    }

最佳答案

你有错误的第二个循环。应该是

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

因为 arrayRow 是第一个索引。在您的代码中,您得到 4 而不是 5,这会导致错误。

此外,您最好对两个循环都使用 for

而且你最好在行循环中有列循环,因为前者是依赖的,或者你应该交换索引;

关于Java 二维数组 for 循环不读取最后一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13793057/

相关文章:

C# .ToString ("X4") 在 Java 中等效

java - mongodb-java-driver 构建在 gradle 上失败,错误代码为 :bson:clirr "Cannot cast object ' file collection'"

java - 如何将 "levels"分配给无环有向图的顶点?

java - 展平不均匀数组 - 越界异常错误

java - 使用二维数组存储线的一部分

java - 使用JDBC和JWT实现Spring OAuth2,并使用基于XML的配置来自定义现有的授权流

java - 斯坦福依赖关系转换工具

java - 3D 数组(一维平面)索引

JavaScript 通过嵌套数组过滤对象数组并检查嵌套数组内的变量匹配

java - 在Java中通过二维数组生成所有路径/路线