简体   繁体   中英

Java lottery program for School Project

i'm really new to java and i don't know if i've done this a good way but i've tried. The program is supposed to generate line of random numbers and after that ask if you want a new one. The part where i can't get it working is the latter. I've tried alot of stuff and i just can't figure it out. (Probably something super simple that i did wrong but i just can't find it).

Here is the code:

    package Lottorivit;

import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.Scanner;

/**
 * Simppeli lottorivi generaattori.
 */
public class Lottorivi {

    public static final int DEFAULT_NUM_TICKETS = 1;
    public static final int DEFAULT_MAX_VALUE = 49;
    public static final int DEFAULT_NUM_VALUES = 7;
    private Random random;

    public static void main(String[] args) {
        Scanner lukija = new Scanner(System.in);
        int numRivit = (args.length > 0) ? Integer.parseInt(args[0]) : DEFAULT_NUM_TICKETS;
        Lottorivi Lottorivi = new Lottorivi();
        for (int i = 0; i < numRivit; ++i) {
            System.out.println("Tässä on rivisi: " + Lottorivi.getNumbers(DEFAULT_NUM_VALUES, DEFAULT_MAX_VALUE));
            System.out.println("Haluatko uuden rivin? Vastaa KYLLÄ tai EI");
            String vastaus = lukija.nextLine();
            if (vastaus.equalsIgnoreCase("KYLLÄ")) {
                System.out.println("Tässä on uusi rivisi: " + Lottorivi.getNumbers(DEFAULT_NUM_VALUES, DEFAULT_MAX_VALUE));
            } else if (vastaus.equalsIgnoreCase("EI")) {
                System.out.println("Hyvää päivänjatkoa!");
            }
        }
    }

    public Lottorivi() {
        this(null);
    }

    public Lottorivi(Long seed) {
        this.random = (seed != null) ? new Random(seed) : new Random();
    }

    public Set<Integer> getNumbers(int numValues, int maxValue) {
        Set<Integer> numbers = new TreeSet<>();
        while (numbers.size() < numValues) {
            numbers.add(this.random.nextInt(maxValue) + 1);
        }
        return numbers;

    }

}

I'm sorry it's in Finnish but you should get the idea of how it works since it's such a simple program. If there's better ways to do it you can always recommend them too. I'm sorry for asking so stupid question. Thanks!

Did you mean like this?

while (true) {
            System.out.println("Tässä on rivisi: " + Lottorivi.getNumbers(DEFAULT_NUM_VALUES, DEFAULT_MAX_VALUE));
            System.out.println("Haluatko uuden rivin? Vastaa KYLLÄ tai EI");
            String vastaus = lukija.nextLine();
            if (vastaus.equalsIgnoreCase("EI")) {
                System.out.println("Hyvää päivänjatkoa!");
                break;
            }
        }

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