Java 我不断得到“不能从静态上下文引用非静态方法”

Java 我不断得到“不能从静态上下文引用非静态方法”,java,Java,该方法应该接受3个参数:String、double和String。然后它将返回一个要在main方法中打印的值。我该怎么做? 这就是我所做的 import javax.swing.JOptionPane; import javax.swing.JTextArea; public class coordinates { public static void main(String args[]) { do

该方法应该接受3个参数:String、double和String。然后它将返回一个要在main方法中打印的值。我该怎么做? 这就是我所做的

    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;

    public class coordinates
    {
        public static void main(String args[])
        {   
            double angle=0, az = 0;
            JOptionPane.showMessageDialog(null, "Hello, this is the 'Bearing to Azimuth' converter.\nFirst, you must input whether the angle is from the North or from the South, input the angle, and then input whether it is East or West.", "Bearing to Azimuth converter", JOptionPane.PLAIN_MESSAGE);
            String ns = JOptionPane.showInputDialog("Input n for North and s for South:");
            String inputangle = JOptionPane.showInputDialog("Input the angle in decimal format:");
            angle = Double.parseDouble(inputangle);
            String ew = JOptionPane.showInputDialog("Input e for East and w for West:");

            convertToSouthAzimuth(ns, angle, ew);


            JOptionPane.showMessageDialog(null, "The converted azimuth is " + az, "Bearing to Azimuth converter", JOptionPane.PLAIN_MESSAGE);
    } //end main method



        public double convertToSouthAzimuth(String ns, double angle, String ew)
        {
            double az = 0;
            if (ns.equals("n")||ns.equals("N")) {
                if (ew.equals("e")||ew.equals("E")) {az= 180+angle;}
                if(ew.equals("w")|| ew.equals("W")){az= 180-angle;}
            }
            if (ns.equals("s")||ns.equals("S")) {
                if (ew.equals("e")||ew.equals("E")) {az= 360-angle;}
                if (ew.equals("w")||ew.equals("W")) {az= angle;}
            }
            return az;
        } //end convertToSouthAzimuth method
    }
更改此项:

public double convertToSouthAzimuth(...) { }

因为main是一个静态方法,您不能调用其中的任何非静态方法。

convertToSouthAzimuth方法尚未声明为静态,因此编译器希望在该类的实例上调用它。但是,您的主方法是静态的,因此没有该类的实例可用

如果ConvertToSouthHazimuth仅使用传递的参数,并且与当前实例无关,则可以将其更改为静态方法:

public static double convertToSouthAzimuth(...) {
您需要将static关键字添加到方法converttosouthazimuth中,因为您是从主方法引用它的,而主方法本身就是一个静态方法

如果您不想将其称为static,可以通过将其包含在新类中来避免这种情况

我故意省略了新类的构造函数

大概是这样的:

public class AzimuthConverter {

   public double convertToSouthAzimuth(String s, Double d, String s){
       return someValue;
   }
}
然后呢

方位变换器myConverter=新的方位变换器

并跟进


double myResult=myConverter.convertToSouthAzimuthvalue

静态方法属于类,而非静态方法属于类实例

因此,您需要执行以下操作

...
new coordinates().convertToSouthAzimuth(ns, angle, ew);
...
或者干脆把它转换成Southazimuth。。。方法static,在这种特殊情况下,它可能是一个更好的选择,因为它在坐标类上没有任何方向双关语,它甚至可以移动到一个helper类,正如@limellights在另一个答案中所建议的那样


干杯,

因为其他人已经回答了您的错误,我想指出代码中的另一个增强。你可以用它来代替

        if (ns.equals("n")||ns.equals("N")) {
可能是

        if (ns.equalIgonreCase("n")) {

首先,您应该阅读关于类、如何实例化类和静态成员的Java基础知识。在掌握了这些概念之后,试着找出程序中的问题。
        if (ns.equalIgonreCase("n")) {