【发布时间】:2019-07-19 04:24:52
【问题描述】:
我正在为我的一个课程编写一个“Nim”程序,其中生成随机数量的石头,然后玩家和计算机轮流从一堆石头中取出 1-3 块石头。移走最后一块石头的玩家输了。
但是,无论该方法内部为计算机生成什么代码,它总是返回 0,因此说计算机从堆中移除了 0 个石头。
(知道这些是两个独立的文件也可能会有所帮助。)
//code code code...
System.out.println("You take " + playertake + " stones. There are " + rockCount + " left");
int computerTake = 0;
nimMethods.computerTake(computerTake);
rockCount = rockCount - computerTake;
System.out.println("The computer takes " + computerTake + " stones. There are " + rockCount + " left");
这是我的方法文件:
public class nimMethods
{
static int computerTake(int y)
{
y = (int)(Math.random()*((1 - 1) + 3 + 1)); //randomly generating a value between 1-3
return(y);
}
}
我坚信这是一个逻辑错误,并且是由于我缺乏对方法的了解。但人们似乎并没有问我在哪里看这个问题。
有人可以帮帮我吗?并解释你的答案,我想学习。
谢谢!
【问题讨论】:
标签: java class methods int return