JAVAWEB猜数字游戏1到100(这是老师给的思路最好按照老师给的思路来)

2025-06-26 06:40:53
推荐回答(2个)
回答1:

public class Test {

public static void main(String[] args) {

int b = (int) (Math.random()*100 + 1);
while (true) {
try {
Scanner in=new Scanner(System.in);
int a=in.nextInt();
if(a < 1 || a > 100) {
System.out.println("范围不合法");
} else if(a > b) {
System.out.println(a + "太大");
} else if (a < b) {
System.out.println(a + "太小");
} else {
System.out.println("你终于猜对了");
break;
}
} catch(Exception e) {
System.out.println("数字格式不合法");
continue;
}
}
}

}

回答2:

私信我啊 我可以帮助你解决