简体   繁体   English

Java多维数组

[英]Java multi dimensional array

I want to search a multi-dimensional array and print the numbers greater than 7 with their locations. 我想搜索一个多维数组,并打印大于7的数字及其位置。

This code compiles and runs without any errors, but doesn't provide any output. 该代码可以编译并运行,没有任何错误,但是不提供任何输出。

Please help me to solve this problem. 请帮我解决这个问题。

class Sarr{

   public static void main(String args[]){    
     int[][] numArray = {{1,2,5,6,4,0},{6,0,1,2},{1,7,3,4},{3,5,6,8,5}};      
     arr(numArray);
   }

   private static void arr(int [][] array){

   int val = 7;

   for (int r = 0; r < array.length; r++) {
        for (int c = 0; c < array[r].length; c++) {

          if (array[r][c] > val){

             System.out.println("Value found was " + val + "["+r+"]"+"["+c+"]");

           }
        }            
    }
  }    
}

您的测试数组没有任何元素> 7 ...

这是因为您要严格查找array[r][c] > 7 ,所以数组中的所有值都不大于7。

The problem is that there is no number greater than 7 in your array. 问题是您的数组中没有大于7的数字。 If you want it to print 7's you will need to change your if statement to 如果要打印7,则需要将if语句更改为

if(array[r][c]>=val) {
    //Print
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM