In: Computer Science
with java code
write The method prints a meaningful title with your name as the author. The method then generates THREE random numbers 0 - 4 and reports how many of the numbers are the same.
2-Use a loop to repeat 10 times.
the output should be like this
Title title title by Your Name 4, 3, 4 two are the same 4, 0, 0 two are the same . . . 2, 0, 4 none are the same 4, 4, 4 three are the same 2, 3, 3 two are the same
Code:
import java.util.Random;
public class Code{
//myLoop is a method
static void myLoop(){
System.out.println("Looper and equality checker by Soligt");
//title with name
int high = 5, a, b, c, i;//variable declaration
Random random = new Random();//random class instance
//the following loop iterates for 10 times from i=0 to i=9
for(i=0; i<10; i++){
a = random.nextInt(high);//generates a random number in the range
of 0 to high=5
b = random.nextInt(high);//generates another random number in the
range of 0 to high=5
c = random.nextInt(high);//generates another random number in the
range of 0 to high=5
//checks if all three random number are equal and prints the
corresponding statement
if(a == b && a == c){
System.out.printf("%d, %d, %d three are the same\n", a, b,
c);
}
//checks if any two random number are equal and prints the
corresponding statement
else if((a == b && a != c) || (a == c && a != b) ||
(b == c && b != a)){
System.out.printf("%d, %d, %d two are the same\n", a, b, c);
}
//if all the above conditions fail, the following in printed
else{
System.out.printf("%d, %d, %d none are the same\n", a, b, c);
}
}
}
//main method calls the myLoop() method
public static void main(String[]args){
myLoop();
}
}
Code in the image:
Please refer to the following image for indentation of the code.
Output: