import java.util.Scanner;
public class ImportStatic {
public static void main(String[] args) {
setNum();
}
private static void setNum() {
int num = (int) (Math.random() * 100) + 1;// 随机数
Integer count = 0;// 计数器
System.out.println("请输入一个整数!");
numberplay(num, count);
}
private static void numberplay(int num, Integer count) {
System.out.println(num);// 随机数测试用
Scanner sc = new Scanner(System.in);// 输入
try {
int number = sc.nextInt();
while (true) {
if (number == num) {//正确答案
System.out.println("恭喜用了" + (++count) + "次猜中");
System.out.print("如果继续请按下任意字母");
number = sc.nextInt();
if (number >= 0 | number < 0) {
break;
} else {
num = (int) (Math.random() * 100) + 1;
numberplay(num, count++);
}
} else {
while (true) {//错误答案循环输入
System.out.println("继续猜吧" + (++count));
number = sc.nextInt();
if (number == num) {
break;
} else if (number > num) {
System.out.println("猜大了-->");
} else if (number < num) {
System.out.println("小了-->");
}
}
}
}
} catch (Exception e) {// 做为输入不是数字返回用
System.out.println("输错-->" + (++count));
numberplay(num, count++);
}
}
}
|
|