简体   繁体   English

我如何将变量传递给类,然后返回给它使用不同值定义的类?

[英]How would I pass a variable through to a class, then back out to the class it was defined in with a different value?

I'm coding a "Nim" program for one of my classes inwhich a random number of rocks is generated, then the player and computer take turns removing 1-3 rocks from the pile. 我正在为我的一个课程编写一个“ Nim”程序,该程序会生成随机数的岩石,然后播放器和计算机轮流从堆中移除1-3块岩石。 The player to remove the last rock loses. 玩家将最后一块岩石移走。

However, no matter what the code generated for the computer inside the method, it would always return 0, and as such say the computer removed 0 rocks from the pile. 但是,无论方法中为计算机生成的代码是什么,它总是返回0,因此计算机从堆中清除了0块岩石。

(It also may help to know that these are two separate files. ) (这也可能有助于了解这是两个单独的文件。

//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");

Here is my methods file : 这是我的方法文件:

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);
   }
}

I have a strong belief that this a logic error, and is coming from my lack of knowledge on methods. 我坚信这是一个逻辑错误,并且是由于我缺乏方法知识。 But people don't seem to be asking this question where I look. 但是人们似乎并没有问我在哪里看这个问题。

Could someone give me a hand? 有人可以帮我吗? And also explain your answer, i'd like to learn. 并解释您的答案,我想学习。

Thanks! 谢谢!

You should do: 你应该做:

computerTake = nimMethods.computerTake(computerTake);

The value of computerTake is not being changed in your code, so it stays 0 as initialized. 您的代码中的computerTake值未更改,因此初始化后保持为0

Not sure why your computerTake() method takes a parameter though. 不确定为什么您的computerTake()方法采用参数。

That makes the code as follows: 这使得代码如下:

System.out.println("You take " + playertake + " stones. There are " + rockCount + " left");

int computerTake = 0;

computerTake = nimMethods.computerTake();

rockCount = rockCount - computerTake;

System.out.println("The computer takes " + computerTake + " stones. There are " + rockCount + " left");

and

public class nimMethods
{
   static int computerTake()
   {
      int y =  (int)(Math.random()*((1 - 1) + 3 + 1)); //randomly generating a value between 1-3
      return(y);
   }
}

This is because Java is Pass by Value : The method parameter values are copied to another variable and then the copied object is passed, that's why it's called pass by value. 这是因为Java是按值传递 :将方法参数值复制到另一个变量,然后传递复制的对象,这就是为什么将其称为按值传递。

So you cannot see "changed" value of y oustide your computerTake method because the value of y was copied. 因此,您无法看到y的 “更改”值取代您的computerTake方法,因为y的值已被复制。

To fix it you can just replace the value of computerTake with your method result which you've returned 要解决此问题,您可以只用返回的方法结果替换computerTake的值

computerTake = nimMethods.computerTake(computerTake);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM