简体   繁体   English

如何在循环外打印语句以避免重复?

[英]How to print statement outside loop to avoid duplicates?

I would like my print statement to be outside the loop so the statement doesn't print the same thing over and over. 我希望我的print语句不在循环中,因此该语句不会一遍又一遍地打印相同的内容。 The for loop below simply checks a number from one array against another to find out how many matches have been found. 下面的for循环仅将一个数组中的一个数字与另一个数组进行比较,以查找已找到多少个匹配项。 Defining the variables above and printing the statements below results in a "variable not initialised error" which is understandable. 定义上面的变量并打印下面的语句会导致“变量未初始化错误”,这是可以理解的。

for (int i = 0; i < 6; i++)
{
    int chkNum = myArray[i];
    int lottMtch = count(chkNum, rndNum);

    if (lottMtch > 0)
    {
        System.out.println(lottMtch + "matches found");
        System.out.print(chkNum);
    }
    else {
        System.out.print("no matches found");
    }
}

Declare the variable before the loop and then do your stuff in the loops, like adding one to the variable if it is found, then print it out afterwards if it is more than 0. Something like this... 在循环之前声明变量,然后在循环中进行操作,例如在发现变量时将其添加到变量中,然后在变量大于0时将其打印出来。类似这样的事情...

int var = 0;
for(...) {
   if(found)
       var++;
}
if(var > 0)
    sysout(var);

Of course this code won't work but it is a start. 当然,此代码将不起作用,但这只是一个开始。 For your learning experience I will let you implement this idea with your code. 为了您的学习经验,我将让您通过代码来实现这个想法。

This is because if you only declare the variables above the loop and only initialize said variables in the loop, when you attempt to print them outside of the loop, there's no guarantee that they would have been initialized. 这是因为,如果仅在循环上方声明变量,并且仅在循环中初始化变量,则当您尝试在循环外打印变量时,无法保证它们将被初始化。

So, perhaps you want something like this: 因此,也许您想要这样的事情:

int lottMtch = 0;

for (int i = 0; i < 6; i++)
{
    int chkNum = myArray[i];
    lottMtch += count(chkNum, rndNum);
    //System.out.print(chkNum); this would not really make sense outside of the loop
}

if (lottMtch > 0)
{
    System.out.println(lottMtch + "matches found");
}
else 
{
    System.out.print("no matches found");
}

this would not really make sense .. if you want than Try this ... 如果您想要的话,这真的没有道理。

        int lottMtch[]=new int[myArray.length];
           Arrays.fill(lottMtch, 0);
            for (int i = 0; i < 6; i++)
            {
                int chkNum = myArray[i];
               lottMtch[i] = count(chkNum, rndNum);

            }
            for (int i = 0; i < 6; i++)
            {
                       if (lottMtch[i] > 0)  
                            System.out.println(lottMtch[i] + " matches found "+ myArray[i]);
            }

If you wan to found just how many match of rndNum in myArray Than try this 如果您rndNummyArray找到rndNum匹配数, rndNum尝试以下操作

Here i assume rndNm is global 在这里我假设rndNmglobal

        int lottMtch=0;

            for (int i = 0; i < 6; i++)
            {

               lottMtch += count(myArray[i], rndNum);

            }

                       if (lottMtch> 0)  
                            System.out.println(lottMtch + " matches found "+ rndNum);

As per discussed in comment Try this .. 根据评论中讨论的尝试此..

Map<Integer,Integer> map = new HashMap<Integer,Integer>();


       for (int i = 0; i < 6; i++)
         {
             Integer chkNum = myArray[i];
            Integer cnt = (Integer)count(myArray[i], rndNum);
              if(cnt>0)
              {
                  if(map.get(chkNum)==null)
                     map.put(chkNum,1);
                  else
                     map.put(chkNum, map.get(chkNum)+1);
              }

         }

         for (Object key : map.keySet()) 
             System.out.println(map.get(key) + " matches found "+key.toString());

如果您打算在循环退出后访问它,则需要在循环外声明一个变量。

You need to initialise variables outside the loop. 您需要在循环外初始化变量。 Try this: 尝试这个:

int chkNum = 0;
int lottMtch = 0;

for (int i = 0; i < 6; i++)
{
    chkNum = myArray[i];
    lottMtch = count(chkNum, rndNum);

}

if (lottMtch > 0)
{
    System.out.println(lottMtch + "matches found");
    System.out.print(chkNum);
}
else {
    System.out.print("no matches found");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM