Sign in to follow this  
lcy

static variable problem

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this