I am attempting to create 100 random numbers between 0 and 99. The numbers should be on only one line and separated by spaces (which is why I used the print() method). I attempt to check my results by opening the new file in a text editor, but instead of getting an output such as...
73 4 66 98 80 7…
(with 100 numbers), I instead only receive one number such as...
56
I can not seem to figure out what I am doing wrong as to why I can't get the loop to repeat 100 times. ANy help would be greatly appreciated.
CODE
public class Numbers {
public static void main(String[] args) throws Exception {
// Create a text file
java.io.File file = new java.io.File("Numbers.txt");
java.io.PrintWriter output = new java.io.PrintWriter(file);
// Write formatted output to the file
for (int i = 0; i <= 100; i++) {
int numbers = (int) (Math.random() * 100);
output.println(numbers + " ");
// Close the file
output.close();
}
}
}