dimanche 12 juin 2016

Java - Why empty output on console?

i've got a problem here:

I was asked to create a collection of random lottery numbers. These numbers should then be ordered from low to high and dublicates shouldn't be put out on console.

It's working so far but the console throws an empty output when the size of the collection is not == 6. I gave an else instruction that the method should then be executed again.

Hope anybody understands and can help.

Thank you!

    import java.util.*;

public class Lotto {
    public static Collection<Integer> tippen() {
        List<Integer> liste = new ArrayList<Integer>();

        while (liste.size() < 6) {
            liste.add((int) (Math.random()*49+1));
        }       

        Collections.sort(liste);

        LinkedHashSet<Integer> lottotipp = new LinkedHashSet<Integer>(liste);
        liste.clear();
        liste.addAll(lottotipp);

        return lottotipp;
    }

    public static void main (String[] args) {

        Collection<Integer> zahlen = tippen();

        if (zahlen.size() == 6) {   
            System.out.println("Ihr Tipp: " + zahlen);
        } else {
            tippen();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire