简体   繁体   English

从方法java返回int值

[英]Returning an int value from a method java

I'm pretty new to java, but I'm trying to make a simulation of the finger game, 'Sticks', using my limited knowledge. 我对Java还是很陌生,但是我正在尝试使用我有限的知识来模拟手指游戏“ Sticks”。 This may not be the neatest, but if you're going to make a suggestion on me to do something, link a page explaining what that thing is, and I'll read it. 这可能不是最整洁的方法,但是如果您要建议我做某事,请链接说明该事情是什么的页面,我会阅读。

Ok, so the issue comes up basically when I call a method to decide who's turn it is and trying to return the value for the "count" up to 5, but it's not returning to main() 好的,所以当我调用一个方法来确定轮到谁并尝试将“ count”的值返回到5时,问题基本上就出现了,但是它没有返回到main()

public static int TurnCalcBB(int PLH, int PRH, int BRH, int BLH, int Death)
{
    //Attacking with bot Right hand
    Random botAtk = new Random();
    if(botAtk.nextInt(2) == 1 && PRH <= 5)
    {
        PRH = BRH + PRH;
        JOptionPane.showMessageDialog(null,"Your right hand is now at " + PRH);
        return PRH;
    } else if(botAtk.nextInt(2) == 0 && PLH <= 5){
        PLH = BRH + PLH;
        JOptionPane.showMessageDialog(null, "Your left hand is now at " + PLH);
        return PLH;
    }
    return Death;
}

Death is there because I was getting an error telling me that I always need to return SOMETHING so I'm returning a static value. 死在那儿是因为我遇到一个错误,告诉我我总是需要返回SOMETHING,所以我正在返回一个静态值。

Basically, the problem is getting PLH (player left hand) or PRH (player right hand) to return to main. 基本上,问题是让PLH(玩家左手)或PRH(玩家右手)返回到主菜单。 If I'm not wrong, they should return as their initial variable name (PL, and PR) with the returned value correct? 如果我没看错,它们应该以初始变量名称(PL和PR)返回,并且返回值正确吗? If not, what can I do to fix this? 如果没有,我该如何解决?

The code is a lot larger than this, and this issue is happening throughout the whole program, so I'm showing just 1 method and assuming they're all the same issue; 代码比这大得多,并且这个问题在整个程序中都发生,因此,我仅展示一种方法,并假设它们都是同一问题。 the methods are almost all the same. 方法几乎都是一样的。

Also, while I'm typing a question already, is nextInt() the best way to do a random number generator? 另外,虽然我已经输入了一个问题,但是nextInt()是执行随机数生成器的最佳方法吗? When I had it as nextInt(1) it was exclusively attacking the left hand, and when I switched it to nextInt(2) now it's attacking both, but occasionally the code... "crashes" (what I mean by crashes is that it generates a number outside of what the If statements are looking for). 当我将其命名为nextInt(1)时,它专门攻击左手,而当我将其切换为nextInt(2)时,它同时在攻击左手,但有时代码会“崩溃”(崩溃的意思是它会生成除If语句所查找内容之外的数字)。 I obviously need to to generate either a 1 or a 2 (or 0 and 1 if 0 counts). 我显然需要生成1或2(如果计数为0,则生成0和1)。

You can change your code to 您可以将代码更改为

public static Integer TurnCalcBB(int PLH, int PRH, int BRH, int BLH, int Death)
{
    //Attacking with bot Right hand
    Random botAtk = new Random();
    if(botAtk.nextInt(2) == 1 && PRH <= 5)
    {
        PRH = BRH + PRH;
        JOptionPane.showMessageDialog(null,"Your right hand is now at " + PRH);
        return PRH;
    } else if(botAtk.nextInt(2) == 0 && PLH <= 5){
        PLH = BRH + PLH;
        JOptionPane.showMessageDialog(null, "Your left hand is now at " + PLH);
        return PLH;
    }
    return null;
}

NOTE: make sure you first check for null values where you call this function. 注意:请确保您首先检查在调用此函数的空值。

You are generating random number twice, this is why you can observe "strange" behvior. 您两次生成随机数,这就是为什么您可以观察“奇怪”行为的原因。

Random botAtk = new Random();
if(botAtk.nextInt(2) == 1 && PRH <= 5) {
  ...
} 
else if(botAtk.nextInt(2) == 0 && PLH <= 5) {
  ...
}

Try generating random only once: 尝试仅生成一次随机数:

Random botAtk = new Random();
boolean right = botAtk.nextInt(2) == 1; // flip coin only once

if(right && PRH <= 5) {
  ...
} 
else if(!right && PLH <= 5) {
  ...
}

I know the answer will not get accepted, because there is an accepted one, but nevertheless: 我知道答案不会被接受,因为有一个被接受的答案,但是:

I suspect that you have a wrong understanding of method parameter passing in Java. 我怀疑您对Java中传递的方法参数有错误的理解。 What I read from your question and comments is that you expect this to work: 我从您的问题和评论中读到的是,您希望它能起作用:

public static int psInt = 0;

static void main() {
    int someNumber = 1;
    int someOtherNumber = 5;

    method1( someNumber, someOtherNumber );
    // You expect "someNumber" to be 6 right now.
    // But in fact, the value will be unchanged.

    // What WILL work: psInt is 0 now
    method3(); // this method will modify the static class var
    // psInt is 5 now.
}


static void method1( int numParam, int someothervalue ){
   numParam = numParam + someothervalue;
}

static void method2( int someNumber, int someothervalue ){
   someNumber = someNumber + someothervalue; // <- same name won't work either!
}

public static void method3(){ 
     psInt = 5; 
}

But in Java method arguments are passed by value. 但是在Java方法中,参数是通过值传递的。 That is: a copy! 那就是:副本! So no matter how you name the variables and arguments, you will never have an "out" argument here. 因此,无论您如何命名变量和参数,这里都永远不会有“ out”参数。

What you can do: 可以做什么:

  1. In a static method, you can use and modify static class variables. 在静态方法中,可以使用和修改静态类变量。
  2. In a non-static method, you can use and modify non-static and static class variables. 在非静态方法中,可以使用和修改非静态和静态类变量。
  3. You can pass a State-Object, of which you can modify field values. 您可以传递一个状态对象,您可以修改它的字段值。
  4. You can return a value. 您可以返回一个值。
  5. ... there are more possibilites. ...还有更多的可能性。 These just to start with. 这些只是开始。

In your case, 4. does not make so much sense, because you wouldn't know if it is the new right or left hand value. 在您的情况下,4.没有太大意义,因为您不知道它是新的右手值还是左手值。

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

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