I get the error, "Cannot make a static reference to the non-static field" about "Scanner sc = new Scanner (System.in);". If I put "static" in front of this line, three errors show stating, "The local variable f (or g/h) may not have been initialized." How do I fix this? I am trying to make three methods about functions and then putting them into a main program. I am using Java.
{code}
package attempt;
import java.util.*;
public class Methods2
{
// f(x) method
static double f (double x) {
return (6 * x - 3);
}
// g(x) method
static double g(double x) {
return (2*x*x - x - 1);
}
// h(x) method
static double h (double x) {
return (f(x) + g(x));
}
Scanner sc = new Scanner (System.in);
public static void main(String[] args) {
double f, g, h, x;
//prints functions
System.out.println("f(x) = 2x + 5");
System.out.println("g(x) = 2x^2 - x - 1");
System.out.println("h(x) = f(x) + g(x)");
//user input
System.out.print("x= ");
x = sc.nextDouble();
//answers
System.out.println("f(x) = " + f);
System.out.println("g(x) = " + g);
System.out.println("h(x) = " + h);
}
}
{code}
{code}
package attempt;
import java.util.*;
public class Methods2
{
// f(x) method
static double f (double x) {
return (6 * x - 3);
}
// g(x) method
static double g(double x) {
return (2*x*x - x - 1);
}
// h(x) method
static double h (double x) {
return (f(x) + g(x));
}
Scanner sc = new Scanner (System.in);
public static void main(String[] args) {
double f, g, h, x;
//prints functions
System.out.println("f(x) = 2x + 5");
System.out.println("g(x) = 2x^2 - x - 1");
System.out.println("h(x) = f(x) + g(x)");
//user input
System.out.print("x= ");
x = sc.nextDouble();
//answers
System.out.println("f(x) = " + f);
System.out.println("g(x) = " + g);
System.out.println("h(x) = " + h);
}
}
{code}