简体   繁体   中英

Java While Loop creating random numbers

Was looking for an answer on here but couldnt really find what I was looking for. So I'm new to learning Java and wanted to make a small lotto simulation where you have 6 random generated numbers from 1-99 and another 6 random generated numbers. I'm trying to create a while loop which prints all 12 numbers, compares them if they are equal or not and printing the amount of tries it took to get all numbers right. If they match up, the loop should end. Now I am kinda stuck because the numbers are getting generated only once. So it generates 2x6 numbers and just keeps using those forever and I can't find a solution. Any ideas?

public class vorgegebeneZahlen {

public static int givenNumb1 = (int) (Math.random() * 99 + 1);
public static int givenNumb2 = (int) (Math.random() * 99 + 1);
public static int givenNumb3 = (int) (Math.random() * 99 + 1);
public static int givenNumb4 = (int) (Math.random() * 99 + 1);
public static int givenNumb5 = (int) (Math.random() * 99 + 1);
public static int givenNumb6 = (int) (Math.random() * 99 + 1);

public static int lottoNumb1 = (int) (Math.random() * 99 + 1);
public static int lottoNumb2 = (int) (Math.random() * 99 + 1);
public static int lottoNumb3 = (int) (Math.random() * 99 + 1);
public static int lottoNumb4 = (int) (Math.random() * 99 + 1);
public static int lottoNumb5 = (int) (Math.random() * 99 + 1);
public static int lottoNumb6 = (int) (Math.random() * 99 + 1);

public static void printNumber() {

    System.out.println("Your given numbers are:");
    System.out.println(givenNumb1 + " " + givenNumb2 + " " + givenNumb3 + " " + givenNumb4 + " " + givenNumb5 + " " + givenNumb6 + "\n");

    System.out.println("Your lotto numbers are:");
    System.out.println(lottoNumb1 + " " + lottoNumb2 + " " + lottoNumb3 + " " + lottoNumb4 + " " + lottoNumb5 + " " + lottoNumb6 + "\n");

}

public static int count = 0;

public static void play() {
    while (lottoNumb1 != givenNumb1 && lottoNumb2 != givenNumb2 && lottoNumb3 != givenNumb3 && lottoNumb4 != givenNumb4 && lottoNumb5 != givenNumb5 && lottoNumb6 != givenNumb6) {
        System.out.println( (count+1) + ". ");
        generateNumber();
        Vergleich.compare();
        count++;
    }
}

then I have a class for comparing

public class Vergleich {

public static void compare() {

    if (lottoNumb1 == givenNumb1 && lottoNumb2 == givenNumb2 && lottoNumb3 == givenNumb3 && lottoNumb4 == givenNumb4 && lottoNumb5 == givenNumb5 && lottoNumb6 == givenNumb6) {
        System.out.println("All numbers match, you won!" + "\n");
    } else {
        System.out.println("Numbers don't match, try again!" + "\n");
    }
}

You can simplify your program by using arrays (instead of individual variables for each of given numbers and lotto numbers) and Random (instead of Math.random ) eg

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

public class Main {
    static int n = 6;
    static int[] givenNumbers = new int[n];
    static int[] lottoNumbers = new int[n];
    static Random random = new Random();

    public static void generateNumbers() {
        for (int i = 0; i < n; i++) {
            givenNumbers[i] = random.nextInt(100);
            lottoNumbers[i] = random.nextInt(100);
        }
    }

    public static void printNumbers() {
        System.out.println("Your given numbers are:" + Arrays.toString(givenNumbers));
        System.out.println("Your lotto numbers are:" + Arrays.toString(lottoNumbers));
    }

    public static void main(String[] args) {
        generateNumbers();
        printNumbers();
    }
}

Output:

Your given numbers are:[58, 9, 68, 33, 53, 6]
Your lotto numbers are:[24, 74, 74, 48, 51, 54]

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