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