How to access variable in one method from another method.?
-
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.
-
Answer:
For your bigWinner method, you are passing in the totalwins and totallost variables, so they must exist in the main function. Just pass those variables in from the main function to the FinalResult function. So in your main, you may have something like this (why does bigWinner return a string? Should probably be void): [...] returnString = bigWinner (winCount, loseCount, drawCount); then later.... returnString = finalResult(winCount, loseCount); [...] Remember, your function only exists in memory as long as that function is running. As soon as it returns it's result, it is cleared from memory along with any variables it may have been using. Think of a function as a black box. You can pass data into it, and it spits data out, but what happens in between that is out of your control. (From the perspective of the main function). Sorry, I know this probably doesn't make a lot of sense...
Shifat Taushif at Yahoo! Answers Visit the source
Other answers
those variables get destroyed as you exit the function ... best option is to make them class level variables so they can be accessed by all methods and initialize them to 0 in your constructor. Which you probably don't have since 90% of the java code I've seen on this site isn't correct.
Cronin
Related Q & A:
- How to access a non static method in an abstract class's static method?Best solution by Stack Overflow
- How to insert data from one table to another?Best solution by Stack Overflow
- How can we get SBJSON connectionDidFinishLoading method inside array data in another class?Best solution by stackoverflow.com
- How to direct emails from one account to another?Best solution by Yahoo! Answers
- How do I output from one form to another in Visual Basic?Best solution by support.microsoft.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.