vendredi 19 novembre 2021

I'm trying to create lottery generator and having trouble getting the generated numbers into an Array

I have to create a lottery generator for college, I am in my first year and struggling to work out how to store random number in the array and how to get the array to check if the number is already in the array and if so to not store it and try again. I would love a bit of advice if possible. This the code i have so far.

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    Random rand = new Random();

    int[] lottoNumbers = new int[10];
    int tenCount = 0;
    int sumOfAll = 0;

    while (tenCount < 10) {
        int lottoRandomNum = rand.nextInt(98) + 1;
        for (int loop = 0; loop < 10; loop++) {
            boolean check = Arrays.asList(lottoNumbers).contains(lottoRandomNum);
            if (check == false) {
                lottoRandomNum = rand.nextInt(98) + 1;
                Arrays.setInt(lottoNumbers, tenCount, lottoRandomNum);
                tenCount++;
            } else {
                Arrays.setInt(lottoNumbers, tenCount, lottoRandomNum);
                tenCount++;
            }

        }

There is a problem with Array.setInt , I have import these.

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;

Any help is great thanks.




Aucun commentaire:

Enregistrer un commentaire