简体   繁体   中英

how would I produce 2 random integers, and then compare them?

I want to produce 2 different random integers using java.util.random. then want to compare those two numbers, for example the code produces 5 and 9. then if one number is bigger than the other, use conditional operators to put the bigger number into variable var1. end result "9 is the bigger number". this is all I have starting out.

 Random rand = new Random();

   
    int n = rand.nextInt(50);
    n += 1;
    System.out.println(n);
   

do I have to create n1 and n2 or can I produce 2 random numbers from the same variable?

Not sure why do you even want to do this that way, but you can do it like this:

Random rand = new Random();
int var1 = Math.max(rand.nextInt(50), rand.nextInt(50));
System.out.println(var1 + " is the bigger number");

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