Java猜数字游戏 计算机产生随机数,两人同时猜,猜中即胜,猜不中,提示是大了还是小了

2025-01-19 22:02:21
推荐回答(2个)
回答1:

Application版本的
import java.util.Random;
import java.util.Scanner;

public class RoundGameTest {

public int getTheNum(){
Random random = new Random();
int roundNum = random.nextInt(10);
return roundNum;
}

public boolean getTheAnswer(int num,int roundNum){
boolean flag = false;
if(num>roundNum){
System.out.println("大了一点,继续努力!");
flag = false;
}else if(num System.out.println("小了一点,继续努力!");
flag = false;
}else{
System.out.println("恭喜你,猜中了!");
flag = true;
}
return flag;
}

@SuppressWarnings("resource")
public static void main(String[] args) {
RoundGameTest test = new RoundGameTest();
int rond = test.getTheNum();//获得随机数
Scanner sc = new Scanner(System.in);
System.out.println("A同学输入:");
int a = sc.nextInt();
boolean boo = test.getTheAnswer(a, rond);
while(true){
if(boo){
break;
}else{
System.out.println("B同学输入:");
}
int b = sc.nextInt();
boolean boo2 = test.getTheAnswer(b, rond);
if(boo2){
break;
}else{
System.out.println("A同学输入:");
a = sc.nextInt();
boo = test.getTheAnswer(a, rond);
}
}
}

}

回答2:

怎么发给你