当我编译程序时,类“ Conversion”的实例返回此错误。

import java.util.Scanner;

public class Convert {

    private class Conversion{

        public String getConversion(int inchInput) {
            int yards = (inchInput - (inchInput % 36)) / 36;
            int feet =  (inchInput % 36) - ((inchInput % 36) % 12);
            int inches = (inchInput % 36) % 12;

            return yards + "yards, " + feet + "feet, and " + inches + "inches.";
        }

    } // end of class Conversion

    public static void main( String[] args ) {
        Scanner scanner = new Scanner(System.in);
        int inchInput;
        Conversion conversion;
        conversion = new Conversion();

        // prompt
        System.out.println("Please enter an amout of inches (integer): ");
        inchInput = scanner.nextInt();
        String output = conversion.getConversion(inchInput);
    } // end of method main()
} // enf of class Convert

最佳答案

Conversion类设置为static,以使该类具有可访问的封闭实例

private static class Conversion {
  ...
}

关于java - 计算类中的“错误:无法从静态上下文引用的非静态变量”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18699419/

10-12 06:00