简体   繁体   中英

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. 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()

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.

Basically, the problem is getting PLH (player left hand) or PRH (player right hand) to return to main. If I'm not wrong, they should return as their initial variable name (PL, and PR) with the returned value correct? 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? 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). I obviously need to to generate either a 1 or a 2 (or 0 and 1 if 0 counts).

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. 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. That is: a copy! So no matter how you name the variables and arguments, you will never have an "out" argument here.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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