How can I generate random integers and then print them?
I have tried the following code,but it does not compile.
import java.util.Random; class Random { Random rand = new Random(); int num=rand.nextInt(40); System.out.println(num); } You should use unique class names unless you feel like referring to the different Randoms by their full package titles.
import java.util.Random; public class MyRandom { public static void main(String args[]) { Random rand = new Random(); int num = rand.nextInt(40); System.out.println(num); } } Save the above as MyRandom.java, then do a;
javac MyRandom.java java MyRandom
mainmethod, and give it another name or prefix the classnames.