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 + "之间的数字!");

}
}
}

}

}

转载于:https://www.cnblogs.com/changemax/p/10015123.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值