java中如何编写一个猜数字的游戏?
展开全部
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;
}
}
}
}
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;
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询