It’s slightly hard to explain but I’m making a basic system were it would have a random chance to output something else, I have a single integer that can be different, and need java to be able to know when this said integer is going to be 580-599. I’m Completely stuck and I’m new to java. (I’m not sure if the title is phrased properly) my code is:
class GenerateRandom { public static void main( String args[] ) { int min = 99; int max = 599; //Generate random int value from 99 to 599 System.out.println("Random value in int from "+min+" to "+max+ ":"); int random_int = (int)Math.floor(Math.random()*(max-min+1)+min); System.out.println(random_int); } }
I thought I could get the “random_int” to be recognized when it’s 580-599 to output something, please help me.
Advertisement
Answer
If you want to include 580 and 599 as part of that range, you can just use an if statement to check:
if(random_int<=599 && random_int>=580) { System.out.println(random_int + " falls between 580 and 599"); }
The &&
is known as a logical AND operator and we use it when we want to check if two conditions are both true in order to do something. In your case, these two conditions are the upper and lower bounds of the range.