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;
}
}