彩票程序号码生成器

我正在为我的班级项目进行彩票模拟,但我无法让它不重复相同的数字。其他一切都工作正常,但我尝试了几种方法让它不重复已经绘制的数字,但我就是无法让它工作。我对此还很陌生。


class Lotto

{

    public static void main(String[]args)

    {

        System.out.print("Hvor mange rekker vil du spille?: ");

        Scanner lesInn= new Scanner(System.in);// Gir brukeren mulighet til å taste inn antall rekker de vil spille. (How many tickets)  

        int nummer = lesInn.nextInt();

        System.out.println("Lotto tallene er: ");

        System.out.println();

        for(int i=0; i<nummer; i++)

        {

            int[] lottoNummer = trekk();

            System.out.print(lottoNummer[1] + " " );

            System.out.print(lottoNummer[2] + " " );

            System.out.print(lottoNummer[3] + " " );

            System.out.print(lottoNummer[4] + " " );

            System.out.print(lottoNummer[5] + " " );

            System.out.print(lottoNummer[6] + " " );

            System.out.print(lottoNummer[7] + " " );

            System.out.print("Tilleggstall:(" + lottoNummer[7] + ")");

            System.out.println();

        }//for

    }//main


    public static int[] trekk()

    {

        int[] lottoNummer = new int[8];

        {

            lottoNummer[1] = (int) ((34 * Math.random()) + 1);

            lottoNummer[2] = (int) ((34 * Math.random()) + 1);

            lottoNummer[3] = (int) ((34 * Math.random()) + 1);

            lottoNummer[4] = (int) ((34 * Math.random()) + 1);

            lottoNummer[5] = (int) ((34 * Math.random()) + 1);

            lottoNummer[6] = (int) ((34 * Math.random()) + 1);

            lottoNummer[7] = (int) ((34 * Math.random()) + 1);

        }

        bubbleSort(lottoNummer);

        return lottoNummer;

    }//trekk


    public static void bubbleSort(int[] x)

    {

        int k, y, i;

        for( y = 1; y <= x.length -1; y++)

        for( i= 0 ; i <= x.length -2; i++)

        {

            if (x[i] > x [i+1])

            {

                k = x[i];

                x[i]= x[i+1];

                x[i+1] = k;

            }// if

        }//for

    }//bubbleSort

}//class



繁花不似锦
浏览 72回答 1
1回答

30秒到达战场

在这种情况下,我更喜欢使用 Stream API。所以我认为应该是这样:&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Hvor mange rekker vil du spille?: ");&nbsp; &nbsp; &nbsp; &nbsp; Scanner lesInn = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int nummer = lesInn.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Lotto tallene er: ");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < nummer; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] lottoNummer = trekk();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(lottoNummer[1] + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(lottoNummer[2] + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(lottoNummer[3] + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(lottoNummer[4] + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(lottoNummer[5] + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(lottoNummer[6] + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(lottoNummer[7] + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Tilleggstall:(" + lottoNummer[7] + ")");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static int[] trekk() {&nbsp; &nbsp; &nbsp; &nbsp; return new Random().ints(1, 35).distinct().limit(8).sorted().toArray();&nbsp; &nbsp; }创建一个随机整数流,其中第一位数字表示最小值,最后一个数字表示最大值。Distinct 用于仅生成唯一的数字,Limit 用于限制数组的大小,Sorted 显然用于排序。然后将其映射到数组并返回。实际上,如果您不介意的话,我想重构一下您的代码,如下所示:public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Hvor mange rekker vil du spille?: ");&nbsp; &nbsp; &nbsp; &nbsp; Scanner lesInn = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int nummer = lesInn.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Lotto tallene er: ");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < nummer; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] lottoNummer = trekk();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int num : lottoNummer) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(num + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Tilleggstall:(" + lottoNummer[lottoNummer.length - 1] + ")");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static int[] trekk() {&nbsp; &nbsp; &nbsp; &nbsp; return new Random().ints(1, 35).distinct().limit(8).sorted().toArray();&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java