Java实现猜数字,附带提示功能。

很简单的一段代码;


package com.changeyd.demo;



import java.util.Random;
import java.util.Scanner;


public class MathDemo {


public static void main(String[] args) {
mathDemo();
}


public static int randomWang() {
return (int) (Math.random() * 10000);


}


public static boolean isNumeric(String str) {
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}

public static void isError() {
mathDemo();
}


public static void mathDemo() {
Scanner scanner = new Scanner(System.in);


System.out.println("***************************  Welcome you play 猜一猜!  ***************************");
System.out.println();
System.out.println();
System.out.println("是否开始游戏?请输入1 or 0(1:是,0:否)");
int isNew = 0;

try {
isNew = scanner.nextInt();
} catch (Exception e) {
System.out.println("不好意思!您的输入有错!请输入 0 or 1 !");
isError();
}

if (isNew == 1) {
System.out.println("new game!");


int[] userInt = null;


int userRandom = 0;
int random = randomWang();

int max = 10000;
int min = 0;


System.out.println("已经生成了一个随机数,开始猜你的数字!(注意:数字范围为0~10000)");
while (true) {


try {
userRandom = scanner.nextInt();
} catch (Exception e) {
System.out.println("不好意思!您的输入有错!请输入 0~10000之间的数字!");
isError();
}


if (userRandom == random) {
System.out.println("恭喜您!good!");
return;
} else {
System.out.println("I'm sorry!");




String is = "";
if (userRandom > random) {
is = "大";
max = userRandom;
}else {
is = "小";
min = userRandom;
}


System.out.println("不好意思! 您猜的数字" + is + "了!");
System.out.println("请输入" + min + "~" + max + "之间的数字!");

}
}
}

}

}

猜你喜欢

转载自blog.csdn.net/qq_40223688/article/details/80057076