1

I understand how to pass a variable to another method and I even learned how to do multiple variables to a single method. My problem is I am trying to make a switch statement, when the user inputs a symptom for digoxin(medication for heart) in the statement I want to award him 10 points store it in a variable, and when the user enters another symptom I want to store that in variable as well in a new method. My problem is that after I send the variable to my method and continue with the program it inevitably resets it to zero, and thus dooming my efforts.

Code:

    switch(input5) {
    case "Vomiting":
        score = 0;
        num = 0;
        score = num + 10;
        getMethod(score,0);
        System.out.println(getMethod(num));
        JOptionPane.showMessageDialog (null,"you're correct")  ;
        break;
    case "Dizziness":
        JOptionPane.showMessageDialog (null,"you're correct")  ;
        score = num + 10;
        getMethod(0,score);
        break;
    case "Confusion":
        JOptionPane.showMessageDialog (null,"you're correct");
        break;
    case "Vision":
        JOptionPane.showMessageDialog (null,"you're correct");
        break;
    default :
        JOptionPane.showMessageDialog (null,"you're Wrong");
        break;
    }
...
static int getMethod(int total) {
    int amount = total;
    int amount2 = total2;
    int result = amount + amount2;
    return result;
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Louis345
  • 700
  • 2
  • 11
  • 38

4 Answers4

2

The problem with this question is that it is so poorly phrased that it is difficult to understand what you actually think is happening. Unless we understand that, it is hard to answer it properly. So I'm just going to point out some things that appear to be errors in your thinking.

  • Variables don't "reset" in Java. In this case that the problem is that your getMethod method doesn't update score.

  • If a method returns a value, and that value isn't assigned to something then it is thrown away. In your case you are not assigning the value returned by getMethod in the places that you are calling it.

  • In Java, arguments are passed to methods "by value". The upshot is that something like this won't work:

    int test = 1;
    increment(test, 2);
    
    public void increment(int value, int by) {
        // FAIL - the local copy of "value" is incremented, but the
        // the original "test" variable is not touched.
        value = value + by;
    }
    

    Note that this has nothing to do with the names of the variable. The issue is that the variable inside the method is not "connected" in any way to the variable used at the call site. The method updates the former ... and not the latter.


A couple things that need to be said about your code:

  • It is important to indent your code consistently. There are style guides that tell you what to do. (We've reindented your code in a way that would be acceptable under most style guidelines.)

  • It is important to use sensible and informative names for things like methods, variables and classes. This helps readers understand what the code author intended the code to do / mean. In your case "getMethod" tells the reader nothing about what the method is supposed to do.

  • Methods should also have javadoc comments that state what they are supposed to do, what the arguments and results mean, and so on.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Sorry, about my poor phrasing of the question. I am just trying to get my variable to hold the value. Then I want to pass the value into my method and generate an average. My overall goal is to create a small quiz on medication for my nursing school. – Louis345 Jun 16 '12 at 04:04
  • OK, but that still doesn't explain your specific thinking ... or event *which* variable you are actually talking about. Anyway, I expect that the points in my answer and others should be sufficient ... if you read them carefully. – Stephen C Jun 16 '12 at 06:55
  • @StephenC the phrase `arguments are passed to methods "by value"` should be `primitive type arguments are passed to methods "by value"` – Shantha Kumara Dec 09 '16 at 04:14
  • @ShanthaKumara - No. >>All<< arguments are passed by value. http://stackoverflow.com/a/40523/139985 – Stephen C Dec 09 '16 at 04:24
0

i think the issue here is that every time you enter something and enter your switch statement, it resets the score to 0.

switch(input5){
             case "Vomiting":
             score = 0;

I think you need to set score to 0 before the first input, and not reset it every time you put in vomiting. I can't exactly follow your code, please link the full class.

Matt Westlake
  • 3,499
  • 7
  • 39
  • 80
0

Try this:

score = getMethod(score, 0);

In java, primitives are "passed by value". The value, not the variable, is passed to the method. Changing the value within the method does nothing to the variable that was used to call the method.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Create a static global variable to maintain or persist the score. This will allow you to make subsequent calls to your method and still keep track of an accurate score.

So, create a global variable public static int score = 0;. Inside of your method you can get rid of the score variable initialization to zero score = 0; since you will use the global score variable.

Donnie
  • 113
  • 1
  • 9