我试图获得 2 个随机数的平均值。平均值应为 30,第一个数字应小于第二个数字。但是,我被困在循环功能中。
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);
}
}
我尝试了上述功能,但是我得到了冗余数据
任何人都可以帮助我
我刚开始学习这门语言