Search code examples
javaif-statementvariablesrandommethods

How do you use variables from one method to another? Is there a way to do so if you're using a variable that's assigned to the Math.random function?


In my example, I used the Math.random function to declare a number that corresponds to a food that the user has been randomly assigned.

But if I were to make another method in which I wanted to refer back to that randomized option, how can I code that? Is it possible to make that happen?

This is what I have so far:

public static void foodMethod (Scanner keyboard) 
    { 
        double foodNum = (Math.random() * 3) + 0 ;
        
        System.out.println ("Now let's see...");
        
        System.out.println (); 
        
        if (foodNum <= 0.9) { 

            System.out.println ("You've decided on a sugary bowl of cereal!"); 
            
            System.out.println ("Although, that headache of yours isn’t going to go away anytime soon."); 

        } 
        if (foodNum >0.9 && foodNum <=1.9) { 
        
            System.out.println ("Hmm..."); 
        
            System.out.println ("Interesting"); 
            
            System.out.println ("Seems like someone decided to reach for the healthy option."); 
            
            System.out.println ("You settled on some yogurt and a tall glass of orange juice. Nice work!"); 
        } 

        if (foodNum >1.9 && foodNum <=3) { 

            System.out.println ("You end up grabbing some crackers and a glass of water to help you swallow some Tylenol to ease that headache of yours."); 
        } 

And I wanted to refer back to the foodNum variable and have it'd print something like, "If you chose the sugary cereal, then you start to feel tired and worn out" or if they were given the yogurt and orange juice option it'd print something like, "You start to feel much better, and your head doesn't hurt as much anymore..."

For some reference, this is the method that I want to add that idea to:

public static void tiredMethod (Scanner keyboard) 
        { 
            System.out.println ("About an hour of being bored..."); 

I didn't necessarily try to code it, as I don't know if it's even possible to code or not.


Solution

  • In order to pass the random number to another method, it needs to be given as an argument. Which means the receiving method needs a double parameter.

    public static void foodMethod(Scanner keyboard) {
        double foodNum = (Math.random() * 3) + 0;
        tiredMethod(keyboard, foodNum);
    }
    
    public static void tiredMethod(Scanner keyboard, double num) {
        System.out.println("Number received from foodMethod: " + num);
    }
    

    Note that with primitives, the value is passed and while you can manipulate it within the new method, the changes won't reflect after the method has completed. If you want to keep changes made to the variable, use an Object ('Double' being the wrapper class for 'double'). Objects are passed by reference.