Look at the Java Lotto generator sauce!

Asked 2 years ago, Updated 2 years ago, 74 views

package lotto;

import java.util.Random;

public class Lotto {

    /**
     * * @param args
     */
    public static void main(String[] args) {
        // // TODO Auto-generated method stub

        //Get a lotto array.
        Lotto lotto = new Lotto();
        int[] lottoArray = lotto.lottoNumGenerator();

        //print the lotto number.
        System.out.println ("==lotto number==");
        for(int i=0;i<lottoArray.length;i++){
            System.out.print(lottoArray[i]+" ");
        }

    }


    int[] lottoNumGenerator(){

                //Create an array to store six numeric variables.
                int lottoNumbers[] = new int[6];

                //Use Random to generate random numbers from 1 to 45 and store them in an array.
                Random random = new Random();

                for(int i =0;i<lottoNumbers.length;i++){
                    lottoNumbers[i]=random.nextInt(45)+1;
                }

                // Double check of random numbers in the array, reset the value.
                boolean duplication = true;

                for(int i =0;i<lottoNumbers.length;i++){
                    for(int j=0;j<lottoNumbers.length;j++){
                        if(i!=j&&lottoNumbers[i]==lottoNumbers[j]){
                            duplication=true;
                            lottoNumbers[i]=random.nextInt(45)+1;
                            break;
                        }else{
                            duplication=false;
                        }
                    }
                    //to reset indefinitely until the number of duplicates is changed i--
                    if(duplication){
                        i--;
                    }
                }


                return lottoNumbers;
    }

}

There are conditions like this. I think number 1 has been solved, so where and what should I add to do the other two or three?

java source

2022-09-22 11:52

1 Answers

How about creating a class called LottoPool as follows?

Member variable: Create a list called pickedNumber as public.

Method


2022-09-22 11:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.