电脑猜测我的号码游戏问题 [英] The computer guess my number game problem

查看:76
本文介绍了电脑猜测我的号码游戏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此程序中,用户必须考虑一个数字并让计算机猜测它。



计算机将询问用户绑定,< br $>


计算机将猜测绑定之间的数字,



用户将键入更高或降低以引导计算机猜出正确的数字,



如果计算机猜到了正确答案,用户将输入是,并打印正确的声明。



以下编码是我正在处理的问题,但是,我遇到了一些问题。请帮帮我!谢谢!



我发现的问题:

1.我不明白如何定义randomInt类中的函数,但在main函数之外。



2.我尝试了程序,但计算机的猜测很奇怪。计算机不会猜测基于我的指南的数字。



In this program, the user has to think of a number and let the computer to guess it.

The computer will ask the user for the bound,

The computer will guess the number between the bound,

The user will type in "higher" or "lower" to lead the computer to guess the correct number,

If the computer guesses the right answer, the user will input "yes", and print a correct statement.

The coding below is what I am working on, However, I got stuck in some problems. Please help me out! Thank you!

Problems that I found:
1. I do not understand how to define the randomInt function inside the class, but outside the main function.

2. I tried the program but the computer's guess is weird. The computer won't guess a number that based on my guide.

import java.util.*;

public class Guess 
{
	public static int randomInt(int min, int max)
	{
		return (int)((Math.random()*((max-min)+1))+min);
			}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in); 
		
		//ask about the lower bound,
			TextIO.putln("Hey there! Welcom to the game!\n"
						+"I'm going to guess a number, and you have to pick it\n"
						+ "and you get to decide the bounds.\n" + 
						"What should the lower bound be?");
			int lowerlimit = in.nextInt();
				
				//if the user enter the number lower than 0,
				while(lowerlimit < 0) 
		        {
					TextIO.putln("ERROR Please enter a number that greater than 0");
					lowerlimit = in.nextInt();
					}
		
		//ask about the upper bound,
				TextIO.putln("Great! How about the upper bound?");
			int upperlimit = in.nextInt();
		
				//if the user enter the number lower than 0,
				while((upperlimit <= lowerlimit)) 
				{
					TextIO.putln("ERROR Please enter a number "
				 		+ "that greater than the lower bound.");
					upperlimit = in.nextInt();
					}

		//Print the range and instruction,	
				TextIO.putln("Ok. In the range between " + lowerlimit +
							" and "+upperlimit+":"+"\nPlease enter 'lower'/'higher' "
							+ "when the number I picked is not correct\n"
							+ "Enter 'yes' when I picked the right number"
							);
		
		//Generate the random number between the range,
		   
		    String feedback = "";
			
		    while (!feedback.equals("yes"))
	        {
		    		Random random = new Random();
	            int randNum = lowerlimit + random.nextInt(upperlimit - lowerlimit + 1);
	            			
	            System.out.println("I think it is " + randNum);
	            		feedback = in.next();

	            //When the computer need to pick a lower number,
	            		if (feedback.equals("lower"))
	            			upperlimit = randNum - 1;

	            // When the computer need to pick a higher number,
	            		else if (feedback.equals("upper"))
	            			lowerlimit = randNum + 1;
	        		}
		    
	        //When the user guesses the correct number,
	        		System.out.println("Yes! Correct!");
	        			in.close();
	    }
	
} // end of Guess class





我尝试过什么:



我搜索了许多类似于我的程序的例子,但我仍然无法修复它我的程序。计算机的猜测仍然没有意义。

这是输出:

(假设用户选择的数字 5

嘿那里!欢迎来到游戏!

我要猜一个数字,你必须选择它

然后你就可以决定界限。

下限应该是多少?

1

太棒了!上限怎么样?

10

好​​的。在1到10之间的范围内:

如果我选择的号码不正确,请输入'lower'/'higher'

当我选择正确的时候输入'yes'号码

我认为它是9

更低

我认为它是8

更低

我认为这是6

更低

我认为它是1

更高

我认为它是1< - 重复

更高

我认为它是4

更高

我认为它是4

更高

我认为它是2< - 没有意义



是的!正确!



What I have tried:

I searched for many examples that are similar to my program, but I still can not fix it my program. The computer's guess still doesn't make sense.
Here is the output:
(assume the number the user picked is 5)
Hey there! Welcome to the game!
I'm going to guess a number, and you have to pick it
and you get to decide the bounds.
What should the lower bound be?
1
Great! How about the upper bound?
10
Ok. In the range between 1 and 10:
Please enter 'lower'/'higher' when the number I picked is not correct
Enter 'yes' when I picked the right number
I think it is 9
lower
I think it is 8
lower
I think it is 6
lower
I think it is 1
higher
I think it is 1<--duplicated
higher
I think it is 4
higher
I think it is 4
higher
I think it is 2<--doesn't make sense
yes
Yes! Correct!

推荐答案

引用:

我试过这个程序,但计算机的猜测是奇怪的。计算机不会猜测基于我的指南的数字。

I tried the program but the computer's guess is weird. The computer won't guess a number that based on my guide.



您的代码没有按照您的预期行事,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么和你的任务是与应该做的事情进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是帮助你向你展示发生了什么。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows /jdb.html [ ^ ]

https: //www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器仅显示您的代码正在执行的操作,您的任务是与什么进行比较应该这样做。


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


Quote:

我不明白如何在类中定义randomInt函数,但在主要功能之外。

I do not understand how to define the randomInt function inside the class, but outside the main function.



为什么不呢?你已经做到了!


Why not? You have done that!

public class Guess 
{
	public static int randomInt(int min, int max)
	{
		return (int)((Math.random()*((max-min)+1))+min);
			}

	public static void main(String[] args) {

但是请自己帮忙并选择缩进样式,然后坚持下去。我不关心它是哪种风格:K& R,Whitesmiths,甚至是可憎的1TB:选择它,并坚持下去。将它们混合在一起并使其全部错误并不会使您的代码易于阅读,从而导致错误。

But do yourself a favour and pick an indentation style, then stick to it. I don;t care which style it is: K&R, Whitesmiths, even the execrable 1TB: pick it, and stick with it. mixing them about and getting it all wrong does not make your code easy to read, and that leads to errors.

public class Guess 
    {
    public static int randomInt(int min, int max)
        {
        return (int)((Math.random()*((max-min)+1))+min);
        }
    public static void main(String[] args) 
        {

更加一致,阅读更清晰(顺便说一下,这叫做Whitesmiths,但是选择一个你或你的导师就好了)



Much more consistent, much clearer to read (That's called "Whitesmiths" by the way, but pick one you or your tutor like)

引用:

我试过这个程序但计算机的猜测很奇怪。计算机不会猜测基于我的指南的数字。

I tried the program but the computer's guess is weird. The computer won't guess a number that based on my guide.

这很奇怪不是错误报告 - 因为当你开始让其他人使用你的软件时你会学到它,它有助于了解它是如何奇怪的是,当它很奇怪时,如果有任何信息涉及。



为什么你宣布一种方法给你一个范围内的随机数,然后忽略它?为什么你使用的代码看起来不像它?



你为什么使用随机数来猜测?这就是为什么它们很奇怪!

你会在现实世界中做到这一点吗?

我?我猜在上限和下限之间:如果范围是1:10,我猜是5.如果那个更低,那就成了下限。如果它更高,那就成了上限。并重复。快速,高效(它被称为Binary Chop,查找它)。



除此之外,调试器在这里是你的朋友。

"It is weird" is not an error report - as you will learn when you start letting others use your software, it helps to know how it is "weird", when it its "weird", and if there are any messages involved.

Why have you declared a method to give you a random number in a range, and then ignored it? And why does the code you do use look nothing like it?

Why are you using random numbers to guess at all? That's why they are weird!
How would you do it in the real world?
Me? I'd guess halfway between the upper and lower limits: if the range is 1:10, I'd guess 5. If that's lower, it becomes the low limit. if it's higher, that becomes the upper limit. and repeat. Quick, and efficient (It's called a Binary Chop, look it up).

Other than that, the debugger is your friend here.

这篇关于电脑猜测我的号码游戏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆