In: Computer Science
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you run the program first time for example it will print 3 then when you run the program for the second time it will give a random number for example 9 but number 7 will not be printed because we need you to do exception for it.
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation
please refer code Images
Typed Code:
//packages
import java.lang.Math.*;
public class Main
{
public static void main(String[] args) {
//taking min value = 1 and max value = 10
int min = 1;
int max = 10;
//taking Random number in given
range
//math.random() is greater than or
equal to 0.0 and less than 1.0
//for example:- random number =
0.1
//0.1*(10-1)+1 ==> 0.1*9+1
==> 10
int rand = (int) ((Math.random() *
(max - min)) + min);
System.out.print("Random Number:
");
//if rand not equal to 7
if(rand != 7)
{
//then printing the number
System.out.println(rand);
}
}
}
//code ended here
Output:
Here Random number is 9, so printed
Here Random number is 7, so it will not be printed
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!