JAVA 设计猜数字小游戏

系统100~999自动生成随机数
用户在界面输入一个数 系统告诉大了或者小了 或者对了 只有10次机会
我写了一个,问题在于
①10次都没猜对,无法显示游戏结束。
②游戏结束后,如何加入 输入Y重新再来 输入N退出游戏
我写的源码放不上来

https://yunpan.cn/cvFNsZ2FLu2J3 (提取码:ebb8)

1、猜10次的问题可以设一个计数器,如‘b',while(b<10){...}这样猜错一次计数器加一,当计数器等于10的时候不再while循环,退出猜数,然后System输出’游戏结束‘字样。
2、加入重新再来和退出游戏可以将程序分为两步份,面版代码while死循环,不断打印规则并scanner获取用户输入的选择(如Y重新再来),然后调用猜数游戏,如果猜对了使用retrun;跳出循环并打印’游戏结束‘字样,猜数游戏调用完毕,面版代码重新循环打印规则并scanner获取输入,又可以再选择重新开始或退出游戏。当退出游戏时使用System.exit(0);结束程序。追问

能帮忙把我的完善一下么。

追答

抱歉,目前在上班,暂时帮不了你。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-10-10


int result;
Scanner in = new Scanner(System.in);
int number = (int) (Math.random() * (900) + 100);
System.out.println("\n***********猜数码小游戏,你hold得住吗?*********");
System.out.println("\n  ********随机数字产生:不告诉你!*********\n");
System.out.println("让我们动动脑筋来猜一猜吧,小提示:他是一个从100到999的整数");
long sTartTime = System.currentTimeMillis();
int count=1;
String  breakFlag="N";
while (true) {
System.out.println("请输入你第" + count + "次的猜测");
result = guess(count);
if (result > number)
System.out.println("不好意思,您所猜的数字大于谜底数字!");
else if (result < number)
System.out.println("不好意思,您所猜的数字小于谜底数字!");
else {
SimpleDateFormat sNowDate = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
long sEndTime = System.currentTimeMillis();
System.out.println("\n  ***********正确答案:" + number
+ "***************\n");
if (count == 1) {
System.out.println("perfect!!恭喜您!一次就中!!");
} else if (count< 10) {
System.out.println("good job! 您总共猜了" + count + "次, 还要继续加油!!");
} else {
System.out.println("not bad! 您总共猜了" + count + "次, 任重而道远啊!");
}
System.out.println("当前时间 :" + sNowDate.format(new Date()));
System.out.println("所用时间 :" + (sEndTime - sTartTime) / 1000
+ "秒");

System.out.print("恭喜挑战成功!是否继续【Y/N】?");
String answer=in.nextLine();
if(breakFlag.equalsIgnoreCase(answer)){
System.out.println("\n欢迎使用猜数系统!");
in.close();
break;
}else{
count=0;
number = (int) (Math.random() * (900) + 100);
}

}

if(count==10){
System.out.print("您10次都没猜对,游戏已结束,是否继续【Y/N】?");
String answer=in.nextLine();
if(breakFlag.equalsIgnoreCase(answer)){
System.out.println("\n欢迎使用猜数系统!");
in.close();
break;
}else{
count=0;
number = (int) (Math.random() * (900) + 100);
}

}
count++;
}

追问

你的这个程序是自动运行的。需要用户输入,再按回车继续的

追答

自动运行是什么鬼

追问

我都没有输入数字。您能给我一个完整版的么。重谢

追答本回答被提问者采纳