简体   繁体   中英

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. The for loop below simply checks a number from one array against another to find out how many matches have been found. 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...

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

Here i assume rndNm is global

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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