- public class Systemin {
- public static void main(String[] args) {
-
- Random r = new Random();
- int num = r.nextInt(101);
- int count = 0;
- String regex = "^[0-9]*$";
- Pattern p = Pattern.compile(regex);
- System.out.println(num);
- boolean flag = false;
- System.out.println("请输猜想的数字(数字为0-100之间,最多10次机会):");
- while(!flag){
- Scanner sc = new Scanner(System.in);
- String inNum = sc.nextLine();
- if(p.matcher(inNum).matches()){
- count++;
- if(count>10 || count ==10){
- flag = true;
- System.out.println("猜想次数到达上线,程序结束。。。");
- }
- if(num==Integer.parseInt(inNum)){
- flag = true;
- System.out.println("恭喜你输入正确,程序结束。。。");
- }else if(num>Integer.parseInt(inNum)){
- System.out.println("输入的数字小于随机数,请重新输入:");
- }else if(num<Integer.parseInt(inNum)){
- System.out.println("输入的数字大于随机数,请重新输入:");
- }
- }else{
- System.out.println("输入的不是数字,不计入猜数成绩,请重新输入:");
- }
- }
- }
- }
复制代码 |
|