Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get java to recognize a certain group of random integers

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading