Java 尼姆循环游戏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20340166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The Game of Nim Loop
提问by PratikM
So, I have to make a Java game in the style of Nim....and I have succeeded in making most of it...I just have a couple of questions that I'm confused on:
所以,我必须以 Nim 的风格制作一个 Java 游戏......而且我已经成功地制作了大部分......我只是有几个让我感到困惑的问题:
Question 1: The program executes fine, and it even runs once, but after going through the code once, the program quits...I need help seeing why it does that (Am I using the wrong loop?)
问题 1:程序执行得很好,它甚至运行了一次,但是在执行一次代码后,程序退出了......我需要帮助看看为什么会这样(我使用了错误的循环吗?)
Question 2: Why does the program always choose the computer to be in the so-called "Smart Mode?"
问题2:为什么程序总是选择电脑处于所谓的“智能模式”?
Any help will be much appreciated. Thank you guys :D
任何帮助都感激不尽。谢谢各位:D
Here is my code:
这是我的代码:
import java.util.*;
public class GameOfNim
{
public static void main (String [] args)
{
Scanner in = new Scanner (System.in);
Random num = new Random ();
int numberLeft = num.nextInt(101-10) + 10;
int computerMode = num.nextInt(1);
int subtraction = numberLeft;
boolean turn = num.nextBoolean();
System.out.println ("The number you start out with is: " + numberLeft);
if (computerMode == 0)
{
System.out.println ("The computer is in smart mode");
}
if (computerMode == 1)
{
System.out.println ("The computer is in dumb mode");
}
while (numberLeft > 1)
{
if (turn == true)
{
System.out.println ("It is your turn...");
System.out.printf ("Please enter the number you wish to take from the pile (Remember it has to be less than " + numberLeft/2 + "): ");
subtraction = in.nextInt();
numberLeft -=subtraction;
System.out.println ("The number left is " + numberLeft);
turn = false;
}
if (turn ==false)
{
System.out.println ("It is the computer's turn...");
if (computerMode == 0)
{
numberLeft = smartComputer(numberLeft);
System.out.println ("The number left is " + numberLeft);
}
if (computerMode == 1)
{
numberLeft -= num.nextInt(numberLeft/2);
System.out.println ("The number left is " + numberLeft);
}
turn = true;
return;
}
}
if (numberLeft <= 1)
{
if (turn = false)
{
System.out.println ("You Win!");
}
else
{
System.out.println ("You're horrible...you lost to a computer.");
}
}
}
public static int smartComputer (int num)
{
int power = 2;
while (power < num)
{
power *=2;
}
power /= 2;
num = power-1;
return num;
}
}
采纳答案by ajb
From the documentationof Random:
从Random的文档中:
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive).
返回一个在 0(含)和指定值(不含)之间均匀分布的伪随机整数值。
"Exclusive" means that nextInt(n)
will return an integer that could be n-1
but can notbe n
. nextInt(n)
has n
different possible results, including 0.
“独占”意味着nextInt(n)
将返回一个可以是n-1
但不能是的整数n
。 nextInt(n)
有n
不同的可能结果,包括 0。
The simple fix is to make it nextInt(2)
, since there are 2 possible modes.
简单的解决方法是 make it nextInt(2)
,因为有 2 种可能的模式。
回答by PakkuDon
Random.nextInt(int n) will return an int value between 0 and n exclusive. Since you've computed computerMode as this:
Random.nextInt(int n) 将返回一个介于 0 和 n 之间的 int 值。由于您已将 computerMode 计算为:
int computerMode = num.nextInt(1);
computerMode will always return 0.
computerMode 将始终返回 0。
If there's only two modes that the computer can be in maybe you can use nextBoolean instead?
如果计算机只能处于两种模式,也许您可以使用 nextBoolean 代替?
回答by Paul Richter
Since you've indicated that you have solved your first question, I'll answer the second.
既然你已经表明你已经解决了你的第一个问题,我就回答第二个问题。
This line:
这一行:
Int computerMode = num.nextInt(1)
Should be:
应该:
Int computerMode = num.nextInt(2)
Or perhaps something like
或者也许像
Boolean computerMode = num.nextBoolean()
According to the documentation for Random#nextInt
:
根据文档Random#nextInt
:
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)...
返回一个伪随机、均匀分布在 0(含)和指定值(不含)之间的 int 值...
Consequently, since you're passing in the value 1, the only possible value that can be returned is 0. I suggested using the nextBoolean
method as well, since it looks like you're using the nextInt
method to generate a Boolean-like value anyways.
因此,由于您传递的是值 1,因此唯一可能返回的值是 0。我建议也使用该nextBoolean
方法,因为看起来您正在使用该nextInt
方法生成类似布尔值的值。