Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the value of a method in another method?

Tags:

java

I'm trying to write a program to generate a random 3 digit number and reverse it, and so I wrote out two methods where getRandomNum generates the number and reverseDigits reverses it. However the 2nd method doesn't take in the random number generated from the 1st method, as the 1st method shows a 3 digit number but the 2nd method shows 0 when running the code.

I've tried looking up how to share variables between methods and it seems that I need to use static variables outside the methods. But it still shows 0 for reverseDigits.

Am I missing something or is there something else to be done?

public class MathTrick
{
    static int upperBound = 999;
    static int lowerBound = 100;
        
        //generate random 3 digit number
    static int getRandomNumber = 0;
    static int mDifference = 0;
    
    public static void main(String[]args)
    {
        getRandomNum();
        reverseDigits();
    }
    
    static void getRandomNum()
    {
        int upperBound = 999;
        int lowerBound = 100;
        
        //generate random 3 digit number
        int getRandomNumber = 0;
        int mDifference = 0;
        while (mDifference <= 1)
        {
            getRandomNumber = (int)(Math.random()*((upperBound-lowerBound)+1)) + lowerBound + 1;
            int x = (int)(getRandomNumber/100);
            int y = getRandomNumber%10;
            mDifference = Math.abs(x-y);
        }
        int m = getRandomNumber;
    
    }
    
    static int m = getRandomNumber;
    static void reverseDigits()
    {
        int a = m, reverseDigits = 0;
        while (a != 0)
        {
            int remainder = a % 10;
            reverseDigits = reverseDigits * 10 + remainder;
            a = a / 10;
        }
        int n = reverseDigits;
    }
}
like image 228
Man Hei Li Avatar asked Jan 21 '26 22:01

Man Hei Li


2 Answers

The direct answer to your question

Replace this line int m = getRandomNumber; with m = getRandomNumber;

Basically, you overshadowed the static variable m with a local variable int m.


A few changes, here they are (my proposal).

Change that both methods to return an integer getRandomNum returns a new random number, and the reverseDigits method returns reversed number. Additionally, reverseDigits get a parameter - a number that should change.

So, after changes.

public static void main(String[] args) {
    int randomNum = getRandomNum();
    int reverse = reverseDigits(randomNum);

    System.out.println(randomNum);
    System.out.println(reverse);
}

static int getRandomNum() {
    // 
    return getRandomNumber;
}

static int reverseDigits(int m) {
    // 
    return reverseDigits;
}

And you can remove other static fields.

like image 63
djm.im Avatar answered Jan 23 '26 10:01

djm.im


Your methods need return statement, this could help you :

public class MathTrick {
    // generate random 3 digit number
    private int getRandomNumber = 0;
    private int mDifference = 0;

    private int m = getRandomNumber;

    public static void main(String[] args) {
        MathTrick application = new MathTrick();
        System.out.println("getRandomNum() return: " + application.getRandomNum());
        System.out.println("reverseDigits() return: " + application.reverseDigits());
    }

    private int getRandomNum() {
        while (mDifference <= 1) {
            int upperBound = 999;
            int lowerBound = 100;
            getRandomNumber = (int) (Math.random() * ((upperBound - lowerBound) + 1)) + lowerBound + 1;
            int x = (int) (getRandomNumber / 100);
            int y = getRandomNumber % 10;
            mDifference = Math.abs(x - y);
        }
        m = getRandomNumber;
        return getRandomNumber;
    }

    private int reverseDigits() {
        int a = m, reverseDigits = 0;
        while (a != 0) {
            int remainder = a % 10;
            reverseDigits = reverseDigits * 10 + remainder;
            a = a / 10;
        }
        return reverseDigits;
    }
}
like image 40
gantoin Avatar answered Jan 23 '26 11:01

gantoin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!