简体   繁体   中英

How to create Random Numbers within a range for a specific amount of time

This is basically a tool teachers would use to generate random numbers for the position everyone is in for presentations perhaps.

It keeps creating infinite loops. What am I doing wrong? Thank you.

import java.util.Random;
import java.util.Scanner;
public class WhoGoesFirst {
    public static void main(String args[]) {
        Random random = new Random();
        Scanner input = new Scanner(System.in);
        int MIN = 1;
        int students = 0;

        System.out.print("How many students do you have?");
        students = input.nextInt();

        int comp = random.nextInt(students - MIN + 1) + MIN;

        for (int number = 0; number <= students; comp++) {
                System.out.println(random);
        }
    }
}

Your number doesn't change in a loop. Try this:

for (int number = 0; number <= students; number++) {
    int comp = random.nextInt(students - MIN + 1) + MIN;
    System.out.println(comp);
}

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