I am trying to get average of 2 random number. the average should 30 and the first number should be less than the second number. However, I’m stuck in the loop function.
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String respond;
System.out.println("enter OK");
respond = user_input.next();
randomAverage();
}
public static void randomAverage(){
Random random = new Random();
int average = 30;
int a = random.nextInt(100); //random range
int b = random.nextInt(100);
System.out.println("random a " +a);
System.out.println("random b "+b);
while(a>b){
a = random.nextInt(100); //random range
b = random.nextInt(100);
System.out.println("random a " +a);
System.out.println("random b "+b);
}
int c = (a+b)/2;
while (c>average || c<average){
a = random.nextInt(100); //random range
b = random.nextInt(100);
System.out.println("random a " +a);
System.out.println("random b "+b);
}
}
I tried the above function, however I got redundant data
anyone can help me
I’m just started to learn this language
You are never updating the value of
c.Your second loop should look something like:
Note as radai mentioned,
Since
(a+b)/2 = 30, if you knowayou can solve forb. No sense burning CPU cycles when simplyb = 60 - a.