简体   繁体   中英

Calling a method from another Java class when return value should change every time it is run in the new program

I'm a beginner with Java and I'm trying to make two different programs but I'm having issues with transferring the data from one to the other.

The first program is like a random number generator. It looks like this (simplified, but same idea):

public class Tester{
  public static double num;
  public static void main(String[] args){
    double n;
    n = Math.random() * 100;
    System.out.println(n);
    num = n;
  }
  public static double gen(){
    return num;
  }
}

Then, I'm trying to call that function and print n random numbers and put them all into a list. That program looks like this:

public class lister{
  public static void main(String[] args){
    //declare
    int n,counter;
    double[] list;
    double num;
    //initialize
    n = Integer.parseInt(args[0]);
    list = new double[n];
    counter = 0;
    double num = Tester.gen();
    //add all generated numbers to a list, print them
    while (counter < n){
      num = Tester.gen();
      list[counter] = num;
      System.out.println(num);
      ++counter;
    }
  }
}

But num just ends up being 0.0 every time. I've tried to not make it static, but that comes with it's own host of issues and, as I understand it, static doesn't mean not changeable.

How do I fix this to make num a new number each time the while loop is run? Is that even possible?

Here is the Suggested Answer:

public class Tester {
public static double gen(){
    double n;
    n = Math.random() * 100;
    return num;
  }
}

public class lister{
  public static void main(String[] args){
    //declare
    int n,counter;
    double[] list;
    double num;
    //initialize
    n = Integer.parseInt(args[0]);
    list = new double[n];
    counter = 0;
    double num = Tester.gen();
    //add all generated numbers to a list, print them
    while (counter < n){
      num = Tester.gen();
      list[counter] = num;
      System.out.println(num);
      ++counter;
    }
  }
}

If you run the list class main(), then the main() method of Tester class isnt invoked, so the Math.random() never runs. try this :

public class Tester{
  public static double gen(){
    return Math.random() * 100;
  }
}

The thing is that you never call Tester.main() in the lister.main() (you just call Tester.gen() method), therefore since an instance variable double like Tester.num is always initialized to 0.0 value, unless you assign it a different value, you are always getting this value in lister.main() each time you use it.

Your code should work by tweaking Tester.gen() a bit so it is the one which actually returns a random number as follows:

public static double gen(){
    return Math.random() * 100;
}

After this, your code should work fine.

Aside from that, the Tester.main() is quite useless since you are already using/running the lister one, and you can use only one main() as entry point for a Java SE application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM