随机1到100(含100)
输入一个数字与其比较
猜错了有相对大小提示
import java.util.Scanner;
public class HiLo {
public static void main(String[] args) {
int x, y, z = 1;// x为随机数 y为输入数 z为是否猜对的监视器
String str = "y";// 控制程序总循环
Scanner scan = new Scanner(System.in);// 输入数字用
Scanner scan1 = new Scanner(System.in);// 输入字符串用
while (str.equalsIgnoreCase("y")) {// 程序总循环
x = (int) (Math.random() * 100 + 1);// 产生随机数字
System.out.println("猜猜看,数字是多少?");
y = scan.nextInt();
if (y > 0 && y < 101) {// 数字输入正确
while (z != 0) {
if (y == x) {
System.out.println("恭喜你猜对了!!");
z = 0;
} else {// 判断大小
if (y > x) {
System.out.println("再小点~");
} else {
System.out.println("再大点~");
}
System.out.println("猜猜看,数字是多少?");
y = scan.nextInt();
}
}
} else {// 数字输入错误
System.out.println("输入数字错误,必须大于0小于等于100");
System.out.println("程序重新启动中");
System.out.println();
}
if (y == x) {// 控制总循环
System.out.println("继续吗?(Y/N)");
str = scan1.nextLine();
}
}
}
}