The title might sound confusing, so here is what I want to do:

I am currently working on a school project, and I have to make a rock, paper, scissors game.
I am almost done and only need one more thing.
I have two methods (bigWinner and finalResult)
In method 1 (bigWinner), I have the instance variables int totalwins, int totallost, and int totaldraw.

      public static String bigWinner (int totalwins, int totallost, int totaldraw) {

         System.out.println ("You won " + totalwins + " games and " + "The Computer won " + totallost + "");

         if (totalwins > totallost ){System.out.print ("Congrats! You won!");}
         if (totalwins < totallost ){System.out.print ("Sorry, you lost.");}
         if (totalwins == totallost){System.out.print ("It's a draw.");}


         return "";

      }

Now, In my second method (finalResult), I have to tell who is the overall winner.

public String FinalResult (int win,int lose) {
win =; //Need Fixing
lose =; //Need Fixing        

if (win >lose ){System.out.print ("Congrats! You won the game!");}
if (win <lose ){System.out.print ("Sorry, you lost the game.");}
if (win == lose ){System.out.print ("It's a draw.");}          
return "";     

}

As I added in the comments in the code, win and lose does not have a value.
What I want to do is give the value of totallost and totalwins in bigWinner to win and lose like this :

win = (VALUE OF totalwins, in bigWinner);
lose = (VALUE OF totallost, in bigWinner);

What could be done?

Thanks in advance.

The three variables you show are parameters to the bigWinner method. They only exist and have values when the method is called and is executing. They go away when the method exits.
The caller of the bigWinner method has the values of the parameters that were passed to bigWinner(). Can you get the values you need there?

are parameters to the bigWinner method. They only exist and have values when the method is called and is executing. They go away when the met

If I made new variables in bigWinner(), would I be able to use them (somehow) in finalResult?

If I did this :

      public static String bigWinner (int totalwins, int totallost, int totaldraw) {

         System.out.println ("You won " + totalwins + " games and " + "Computer Wins " + totallost + "");

         if (totalwins > totallost ){System.out.print ("Congrats! You won!");}
         if (totalwins < totallost ){System.out.print ("Sorry, you lost.");}
         if (totalwins == totallost){System.out.print ("It's a draw.");}

         int final_win = totalwins;
         int final_lost = totallost;
         int final_draw = totaldraw;

         return "";

      }

Edited 4 Years Ago by shifat96

All local variables in a method go away when the method exits.

The caller of the method has the values you want.

You said that totalwins, totallost, and totaldraw are instance variable, could you not call it in the method like object.getTotalWins? (I am assuming they are in the same class.)

If they are not in the same class, could you not add those values into the method as they are passing by value in the bigWinner method? like

public String FinalResult (int win,int lose, int totalwins, int totallost, int totaldraw)

It would help if you at least let us know where the totalwins, totallost, and totaldraw variable is and where the 2 method is.

you can also have get methods that just return the value of those variables, one method for each variable, and call those functions to get the value

Edited 4 Years Ago by corby

This article has been dead for over six months. Start a new discussion instead.