簡體   English   中英

如何只從for循環中打印一次語句 - Java

[英]How to print a statement from a for loop only once - Java

iv得到一個循環,檢查兩個數組中的值。 如果找到匹配值,則將這些值打印到控制台。

我還提供了一個打印語句,僅當在任何地方找不到匹配項時才打印。

public class MatchTest 
{
   public static void main(String []args)
{
          boolean matchFound=true;
          int [] x = {4,5,6,1,2};
          int [] y = {1,2,3};


          getMatchingNumbers(x,y, matchfound);

}
//method to check numbers in each array and prints the numbers that match ( if found)
   public static void getMatchingNumbers(int [] a, int []b, boolean c)
   {

          for(int i : a)
          {
          for (int j : b)
            {
             if (i==j) 
              System.out.println("matching numbers are " + i);
             else 
               c = false;
             }
          }
// how do i get this to print ONLY if no matches are found any where
          if (c == false)  
          System.out.println("no matches found");   
      }
}        

目前,如果傳入的數組包含一些匹配的數字和一些不匹配的數字,我仍然得到沒有匹配的消息。 而不是這個,我希望沒有匹配發現只有在任何地方都沒有匹配的情況下才打印一條消息。

我認為這應該是一個簡單的修正,但我不知道我哪里出錯了。

建議贊賞。

您將c設置為false,並且僅在數字匹配時才將其設置為true

public static void getMatchingNumbers(int [] a, int []b, boolean c){
        c = false;
        for(int i : a){
            for (int j : b){
                if (i==j){
                    System.out.println("matching numbers are " + i);
                    c = true;
                }
            }
        }
        if (!c)  System.out.println("no matches found");
    }

所以你找到所有的比賽。

當你在if語句中返回時:

if(i==j){
    System.out.println("matching numbers are " + i);
    return;
}

你只找到第一場比賽。

你只需要在開始時將boolean matchFound設置為false。 然后在方法getMatchingNumbers中,將c = true添加到if(i == j)塊。 因為不需要,刪除else塊。

public class MatchTest {
   public static void main(String []args){
          boolean matchFound=false;//set this to FALSE at beginning
          int [] x = {4,5,6,1,2};
          int [] y = {1,2,3};


          getMatchingNumbers(x,y, matchfound);

}
//method to check numbers in each array and prints a the numbers that match ( if found)
   public static void getMatchingNumbers(int [] a, int []b, boolean c){

          for(int i : a){
          for (int j : b){
//Modified the if block and removed the else block
          if (i==j){
              System.out.println("matching numbers are " + i);
              c=true;
          }

          }
   }

          if (c == false)  System.out.println("no matches found");


          }
}    
public class MatchTest {



  public static void main(String[] args) {
     boolean matchFound = false;
     int[] x = {
                4,
                5,
                6,
                1,
                2
                  };
    int[] y = {
                1,
                2,
                3
                  };


    getMatchingNumbers(x, y, matchFound);

 }
    //method to check numbers in each array and prints a the numbers that      match ( if found)
 public static void getMatchingNumbers(int[] a, int[] b, boolean c) {


   for (int i: a) {
      for (int j: b) {

         if (i == j) {
             System.out.println("matching numbers are " + i);
             c = true;
         }

      }
   }
   // how do i get this to print ONLY if no matches are found any where
   if (!c) System.out.println("no matches found");


   }
}`

我在代碼中做了很小的改動。你的代碼是糟糕的編碼風格。 為什么你傳遞一個布爾函數? 沒有必要的 。在Java中,你通過布爾Ç到method.Actually它創建局部變量布爾內部c方法和復制傳遞的值。

這樣做,我很確定您的代碼將在沒有任何問題的情況下運行

public static void getMatchingNumbers(int [] a, int []b, boolean c)
{
      int flag=0;
      for(int i : a)
      {
        for (int j : b)
            {
                if (i==j) 
               {
                    System.out.println("matching numbers are " + i);
                    c=false;//will turn c==false every time if a match is found
                }
            }
    }
       // change your code like this
      if (c)  
        System.out.println("no matches found");
  }       

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM