non static/ static variable error

2019-07-24 15:27发布

问题:

I am making a program that can take judge scores and give an average and one of my variables I think I did wrong. I get the error message:

non-static variable difficulty cannot be referenced from a static context
difficulty = keyboard.nextDouble;

I have tried moving it above the main method as well as also adding "static" to it but it still seems to give me problems. Is there something else that I can do? Here is part of the code for it

import java.util.Scanner;
public class CH6PA
{
private double difficulty;
private int score;
private double[] average;

public static void main(String[]args)
{
    Scanner keyboard = new Scanner (System.in);

    do
    {
        System.out.println("Enter the level of difficulty (1.2-3.8)");
    difficulty = keyboard.nextDouble;
    }
        while (difficulty>1.2 || difficulty<3.8);


    int judge = new int[7];


    for(int i = 0; i<judge.length; i++)
    {

回答1:

The other answers are around changing things to static, you should avoid having mutable state in static variables this is 101 of programming - it's essentially global variables.

import java.util.Scanner;
public class CH6PA
{
private double difficulty;
private int score;
private double[] average;

public static void main(String[]args)
{
    CH6PA ch6pa = new CH6PA();
    ch6pa.doSomething();
}

private void doSomething() {
    Scanner keyboard = new Scanner (System.in);

    do {
        System.out.println("Enter the level of difficulty (1.2-3.8)");
        difficulty = keyboard.nextDouble;
    } while (difficulty>1.2 || difficulty<3.8);

    int judge = new int[7];

    for(int i = 0; i<judge.length; i++) {
    }


回答2:

Be careful with the do { } while();. Your condition is going to be always true, creating an endless loop:

     while (difficulty>1.2 || difficulty<3.8)

Ex:

    1.0 (false || true ) => true

    4.0 (true || false ) = > true

    2.0 (true || true) => true

Instead, define your condition as:

    while (difficulty<1.2 || difficulty>3.8)

And good luck for the rest!



回答3:

The problem as the compiler suggested is that you are using the difficulty instance variable in a static context (ie: not as part of an instance). What you need to do, is if you believe difficulty should be the same in all instances of the class CH6PA then I would set the difficulty to static by putting the static keyword before it in the declaration.



回答4:

The reason you are getting this error is you are referencing the difficulty field in the main method which is declared static. But difficulty is defined as non-static (an instance field). So difficulty is part of a CH6PA instance. But the main method is not. You can only access instance variables from instance methods.



回答5:

As Reseter said above, your difficulty variable should be declared static because of your static method does not have an access to the non-static property.

You may find complete explanation here: http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html



标签: java static