繁体   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