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

时间:2022-10-24 23:44:45

I am searching the 2d array for 3 numbers and the third number is not in the array and when it outputs to the user that the number is not in the array it prints it 10 times and im guessing because the for loop goes up to 10. How can I get the statement "8675 is not in the array" to only print out 1 time.

我正在搜索2d数组中的3个数字,第三个数字不在数组中,当它向用户输出数字不在数组中时,它打印10次并且我猜测因为for循环上升到10。如何才能将“8675不在数组中”的语句只打印出1次。

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");
             }               
        }
    }
}
}

The output that i get is

我得到的输出是

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 个解决方案

#1


1  

Your way:

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");
 }    

"found" will only be true, if you found the value. If you found the value you can "leave" your loops using the break command. It quiets the outer for looop using the lable "outer". You can change the lable however you want.

如果找到了价值,“发现”只会是真的。如果找到了值,则可以使用break命令“离开”循环。它使用标签“外部”使外部平静。您可以根据需要更改标签。

After that, you can just check the boolean, if you found something and print the Text you want.

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

Better way:

A better way to to it would be to write a function:

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

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

You can now call that function like this:

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

boolean hasValue = searchInArray(oned, 8227);

The function is iterating over every int array in your 2d array using a for loop. Inside the for loop it is iterating over each element of the 1d int array. If the element is equevalent to the one you search it will return true. Otherwise it will return false after it searched the whole 2d array.

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

You can now use the variable hasValue to print your text:

您现在可以使用变量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");
 }

#2


0  

Somthing like this :

像这样的东西:

   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");
        }
      }

    }
}

#3


0  

You need to add two breaks to the code to break the for loopsif for循环中的if语句多次打印

您需要在代码中添加两个中断来打破for循环

#1


1  

Your way:

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");
 }    

"found" will only be true, if you found the value. If you found the value you can "leave" your loops using the break command. It quiets the outer for looop using the lable "outer". You can change the lable however you want.

如果找到了价值,“发现”只会是真的。如果找到了值,则可以使用break命令“离开”循环。它使用标签“外部”使外部平静。您可以根据需要更改标签。

After that, you can just check the boolean, if you found something and print the Text you want.

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

Better way:

A better way to to it would be to write a function:

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

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

You can now call that function like this:

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

boolean hasValue = searchInArray(oned, 8227);

The function is iterating over every int array in your 2d array using a for loop. Inside the for loop it is iterating over each element of the 1d int array. If the element is equevalent to the one you search it will return true. Otherwise it will return false after it searched the whole 2d array.

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

You can now use the variable hasValue to print your text:

您现在可以使用变量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");
 }

#2


0  

Somthing like this :

像这样的东西:

   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");
        }
      }

    }
}

#3


0  

You need to add two breaks to the code to break the for loopsif for循环中的if语句多次打印

您需要在代码中添加两个中断来打破for循环