简体   繁体   English

如何只从for循环中打印一次语句 - Java

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

iv got a loop that checks the values in two arrays. iv得到一个循环,检查两个数组中的值。 in the event that matching values are found those values are printed to the console. 如果找到匹配值,则将这些值打印到控制台。

I've also included a print statement that is intended to print ONLY when no matches are found anywhere. 我还提供了一个打印语句,仅当在任何地方找不到匹配项时才打印。

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

At the moment, if the arrays passed in contain some numbers that match and some that don't match, I'm still getting the no matches found the message. 目前,如果传入的数组包含一些匹配的数字和一些不匹配的数字,我仍然得到没有匹配的消息。 instead of this, I want the no matches found a message to print ONLY if no matches exist anywhere. 而不是这个,我希望没有匹配发现只有在任何地方都没有匹配的情况下才打印一条消息。

I think this should be a simple correction but I cant see where I'm going wrong. 我认为这应该是一个简单的修正,但我不知道我哪里出错了。

suggestions appreciated. 建议赞赏。

You set c to false and only set it true if numbers match 您将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");
    }

So you find all matches. 所以你找到所有的比赛。

When you return in the if statement: 当你在if语句中返回时:

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

you find only the first match. 你只找到第一场比赛。

You just need to set the boolean matchFound to false at beginning. 你只需要在开始时将boolean matchFound设置为false。 Then in the method getMatchingNumbers, add c=true to the if(i==j) block. 然后在方法getMatchingNumbers中,将c = true添加到if(i == j)块。 Remove the else block as it is unneeded. 因为不需要,删除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");


   }
}`

I made very small change in code.Your code is bad style of coding. 我在代码中做了很小的改动。你的代码是糟糕的编码风格。 Why you passed a boolean to function? 为什么你传递一个布尔函数? not necessary .In Java ,that you passed boolean c into method.Actually it creates local variable boolean c inside method and copy the passed value. 没有必要的 。在Java中,你通过布尔Ç到method.Actually它创建局部变量布尔内部c方法和复制传递的值。

Do this and I'm pretty sure that your code will run without any prolem . 这样做,我很确定您的代码将在没有任何问题的情况下运行

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.

相关问题 如何在 while 循环中只打印一次语句? - How do I only print statement once in a while loop? Java- For循环,带有带默认情况的switch语句。 如何获得默认情况下仅打印输出一次? - Java- For loop with a switch statement containing default case. How can I get the default case to print the output only once? Java for循环使用if语句仅迭代一次 - Java for loop with if statement only iterating once 如何在for循环中检查条件并打印一次语句? - How to check condition in a for loop and print the statement once? 如何仅从嵌套的 for 循环中打印一次结果 - How can I print a result from a nested for loop only once 如何从嵌套在 while 循环中的 if 语句定期打印到终端(Java) - How to periodically print to terminal from an if statement that is nested in a while loop (Java) 如何仅使用for循环和if在java中只打印一次重复值 - How to print duplicated value only once each using only for loop and if in java 当要求用户在Java中循环输入一些字符串时,如何仅保留文本打印一次 - How to keep Text print only once when asking a user to re-enter some string in loop in Java 对于循环打印数组中的值总和一次,不断重复 - For loop print sum of values from array only once, keeps repeating 如何使用Java中的嵌套循环仅打印一次数字? - How to print single number only once using nested loops in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM