if for循环中的if语句多次打印

时间:2017-03-10 14:07:57

标签: java arrays if-statement for-loop multidimensional-array

我在2d数组中搜索3个数字,第三个数字不在数组中,当它向用户输出数字不在数组中时,它会打印10次并猜测因为for循环上升10.如何才能获得“8675不在数组中”的声明,只打印一次。

public class MultiDimensionalArray {

public static void main(String[] args) {

    int[][] oned = {
            { 1115, 7307, 1004, 8820, 4322, 2286, 6183, 8455, 5569, 9930 },
            { 1155, 7749, 8582, 1180, 4463, 3107, 8838, 9842, 2308, 3453 },
            { 6229, 5449, 1967, 2501, 9610, 5600, 6996, 7375, 5629, 35 },
            { 6677, 2464, 5017, 5881, 639, 2772, 3465, 8718, 7747, 5621 },
            { 1646, 8533, 4250, 8119, 8163, 1236, 4433, 4093, 7834, 3037 },
            { 7069, 6522, 9604, 1609, 5725, 6255, 438, 274, 7978, 3358 },
            { 6631, 3401, 5975, 108, 3696, 2773, 1697, 9803, 7056, 4996 },
            { 7109, 4895, 5930, 7634, 7070, 5265, 7456, 5223, 9725, 368 },
            { 1201, 7776, 9000, 8654, 9635, 922, 2932, 4814, 1624, 1062 },
            { 7561, 6587, 7398, 4254, 5797, 7325, 4368, 5830, 8937, 5726 },
            { 7740, 8238, 7761, 6142, 4643, 7416, 2062, 5563, 1298, 7899 },
            { 1868, 6088, 3071, 7563, 7780, 2714, 7081, 2565, 3086, 766 },
            { 2284, 9931, 8664, 7248, 6768, 5657, 8404, 807, 7357, 2204 },
            { 9911, 6832, 8167, 546, 2709, 2046, 8465, 4171, 1841, 6106 },
            { 2123, 9005, 406, 6873, 3848, 4760, 2912, 1504, 9052, 270 },
            { 8700, 8182, 1153, 1154, 9288, 8227, 6165, 7257, 7908, 1769 },
            { 7355, 3880, 390, 1496, 6984, 7553, 981, 8049, 6948, 7312 },
            { 830, 4777, 5100, 897, 9941, 8513, 9318, 3146, 5298, 8452 },
            { 6678, 6535, 1471, 5225, 5513, 1912, 624, 8802, 5331, 4675 },
            { 4916, 2517, 4604, 4947, 9973, 9347, 9390, 8633, 60, 8983 },
            { 9977, 2505, 8436, 1285, 472, 568, 8696, 5198, 5630, 5087 },
            { 6287, 4834, 6184, 3761, 7922, 3163, 6836, 6621, 3338, 6575 },
            { 7105, 5863, 5113, 1346, 1223, 7733, 1323, 2301, 3021, 8612 },
            { 2976, 282, 271, 8111, 1320, 3441, 7129, 513, 4564, 7278 },
            { 3916, 7150, 9606, 8058, 7533, 8106, 539, 977, 32, 1074 },
            { 5859, 6361, 7489, 8347, 9441, 8281, 7728, 7944, 5272, 1598 },
            { 6078, 4624, 634, 9183, 7772, 6187, 3565, 4912, 2875, 8405 },
            { 1031, 1679, 8287, 689, 4855, 6386, 8616, 8608, 2842, 4986 },
            { 3321, 5150, 1410, 3159, 1328, 30, 191, 7133, 2797, 5334 },
            { 8610, 5512, 8141, 1398, 5918, 2641, 9014, 4475, 4590, 8672 } };

    // Is 8227 in the array?

    int number = 8227;
    for (int row = 0; row < 10; row++) {
        for (int column = 0; column < 30; column++) {
             if (oned[column][row] == number)
             {
                 System.out.println("8227 is in the array");
             }
        }
    }

// Is 9911 in the array?

    int check = 9911;
    for (int row = 0; row < 10; row++) {
        for (int column = 0; column < 30; column++) {
             if (oned[column][row] == check)
             {
                 System.out.println("9911 is in the array");
             }
        }
    }

// Is 8675 in the array?

    int look = 8675;
    for (int row = 0; row < 10; row++) {
        for (int column = 0; column < 30; column++) {
             if (oned[column][row] == look)
             {
                 System.out.println("8675 is in the array");
             } 
             else if (oned[column][row] != look)
             {
                 System.out.println("8675 is not in the array");
             }               
        }
    }
}
}

我得到的输出是

8227 is in the array

9911 is in the array

8675 is not in the array

8675 is not in the array

8675 is not in the array

8675 is not in the array

8675 is not in the array

8675 is not in the array

8675 is not in the array

8675 is not in the array

8675 is not in the array

8675 is not in the array

3 个答案:

答案 0 :(得分:1)

您的方式:

int look = 8675;
boolean found = false;
outer:
    for (int row = 0; row < 10; row++) {
        for (int column = 0; column < 30; column++) {
             found =(oned[column][row] == look);
             if(found){
                 break outer;
             }
         }
     }

 if(found){
       System.out.println("8675 is in the array");
 }else{
       System.out.println("8675 is not in the array");
 }    

&#34;实测&#34;只有你找到了价值才会成真。如果你找到了价值,你可以&#34;离开&#34;你的循环使用break命令。它使用标签&#34;外部&#34;来平息外部的looop。你可以随意改变标签。

之后,您可以检查布尔值,如果找到了某些内容并打印了所需的文本。

更好的方式:

更好的方法是编写一个函数:

public boolean searchInArray(int[][] src, int find){
    for(int[] row : src){
        for(int num : src){
            if(num == find){
                return true;
            }
        }
    }
    return false;
}

您现在可以像这样调用该函数:

boolean hasValue = searchInArray(oned, 8227);

该函数使用for循环遍历2d数组中的每个int数组。在for循环中,它迭代1d int数组的每个元素。如果元素与您搜索的元素相符,则返回true。否则它会在搜索整个2d数组后返回false。

您现在可以使用变量hasValue来打印文本:

boolean hasValue = searchInArray(oned, 8227);
if(hasValue ){
   System.out.println("8227 is in the array");
}else{
   System.out.println("8227 is not in the array");
 }

答案 1 :(得分:0)

像这样:

   public void searchInt(int number){

    int[][] array= {
        {1115, 7307, 1004, 8820, 4322, 2286, 6183, 8455, 5569, 9930},....};

    for (int[] nmbres: array) {
      for (int num : nmbres) {
        if (number== num) {
          System.out.println(number +"is in the array");
        }
      }

    }
}

答案 2 :(得分:0)

您需要在代码中添加两个中断来中断for循环{{3}}