Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

c# - Lottery draw has to many rights each time

What is the error in my code? It counts the correct numbers wrong. Input is 7 numbers in an array Then we have how many random draws it shall be We also have an array with the random numbers that are correct each time

private void CheckLotto()
    {

        Array.Sort(myLotto);
        Array.Sort(SelectedNumbers);
        sevenRatt = 0;
        sixRatt = 0;
        fiveRatt = 0;

        //How many times should we have a draw?
        for (int k = 0; k < howMany; k++)
        {
                int match = 0;

                //Got throw selctedNumbers array
                for (int i = 0; i < SelectedNumbers.Length; i++)
                {
                    //Go throw mylotto array
                    for (int j = 0; j < myLotto.Length; j++)
                    {
                        //Are thye same?
                        if (SelectedNumbers[i] == myLotto[j])
                        {
                            Console.Write("{0}  ", SelectedNumbers[i]);
                            match++;
                        }//if

                        switch(match)
                        {
                            case 5:
                                fiveRatt++;
                                break;
                            case 6:
                                sixRatt++;
                                break;
                            case 7:
                                sevenRatt++;
                                break;
                            default:
                                break;
                           }// switch

                    }//for
                }//for
            }//try
          
        
    }
question from:https://stackoverflow.com/questions/65902973/lottery-draw-has-to-many-rights-each-time

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So the problem is that the switch is in the wrong spot -- it is in the inner loop (which does a count how many correct numbers you got). You need it to be outside that loop and in the loop (which loops over how many to test).

private void CheckLotto()
{

    Array.Sort(myLotto);
    Array.Sort(SelectedNumbers);
    sevenRatt = 0;
    sixRatt = 0;
    fiveRatt = 0;

    //How many times should we have a draw?
    for (int k = 0; k < howMany; k++)
    {
            int match = 0;

            //Got throw selctedNumbers array
            for (int i = 0; i < SelectedNumbers.Length; i++)
            {
                //Go throw mylotto array
                for (int j = 0; j < myLotto.Length; j++)
                {
                    //Are thye same?
                    if (SelectedNumbers[i] == myLotto[j])
                    {
                        Console.Write("{0}  ", SelectedNumbers[i]);
                        match++;
                    }//if

                }//for
            }//for
            
            switch(match)
            {
               case 5:
                  fiveRatt++;
                  break;
               case 6:
                  sixRatt++;
                  break;
               case 7:
                  sevenRatt++;
                  break;
            }// switch

   } // for main        
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...