Jump to content
  • Advertisement
Sign in to follow this  
lcy

static variable problem

This topic is 4745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

task3.java:19: non-static method bernoulli(double) cannot be referenced from a s tatic context how to solve this problem ? the return value must in non-static and the main must in static. below is my source code public class task3 { public boolean bernoulli(double Probability) { return (Math.random()) / (double)(Math.random()) < Probability; } public static void main(String[] args) { int N = 3; int T = 20; double Answer; double P; String output = ""; for(int i = 0; i < N; i++) { output += "\n"; for(int j = 0; j < T; j++) { Answer = (bernoulli(P) ? 'W' : 'L'); output += Answer; } } } }

Share this post


Link to post
Share on other sites
Advertisement
I'm guessing you will need:
public static boolean bernoulli(double Probability)

or you can just not make your main function static [wink]

Share this post


Link to post
Share on other sites
I don't think you have the option of not making Main static in Java. Just make bernoulli static and call it like so: task3.bernoulli(...). If you don't make a function static you have to create an instance of it (task3 task = new task3()) and then call the method(task.bernoulli(...)).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!