0

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);
        }
    }
}

1 Answers1

0

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);
}
Valijon
  • 12,667
  • 4
  • 34
  • 67
ka4eli
  • 5,294
  • 3
  • 23
  • 43
  • I changed it but now it keeps looping numbers between 1 and whatever I typed infintely. –  Feb 07 '16 at 20:06
  • This fixed it but how does the comp variable know how many times to loop for. –  Feb 07 '16 at 20:10
  • not comp, but number. You loop until number>students – ka4eli Feb 07 '16 at 20:12
  • Also, it is creating duplicates. What can I do to prevent duplicate numbers from being generated? –  Feb 07 '16 at 20:14
  • http://stackoverflow.com/questions/196017/unique-non-repeating-random-numbers-in-o1 – ka4eli Feb 07 '16 at 20:16