Method calling error [closed]

2019-09-25 07:11发布

问题:

I need to use two methods in my program which converts temperatures I have a problem with calling my method here is my code:

import java.io.*;
import javax.swing.JOptionPane;

public class Converter {
    public static void main(String[] args) throws Exception{
        //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String unit = JOptionPane.showInputDialog("Enter unit F or C: ");

        String temp1 = JOptionPane.showInputDialog("Enter the Temperature: ");


        double temp = Double.valueOf(temp1).doubleValue();




        public static double convertTemp(){
         if((unit.equals("F"))||(unit.equals("f"))){
        double c= (temp - 32) / 1.8;
        JOptionPane.showMessageDialog(null,c+" Celsius"));
        }

         else if((unit.equals("C"))||(unit.equals("c"))){
        double f=((9.0 / 5.0) * temp) + 32.0;
        JOptionPane.showMessageDialog(null,f+" Fahrenheit");

}
}
}

回答1:

 public static double {
         if((unit.equals("F"))||(unit.equals("f"))){
        double c= (temp - 32) / 1.8;
        JOptionPane.showMessageDialog(null,c+" Celsius");
        }

is not valid Java. You need to create a method, outside the main method

public static double convertTemp(){
...
}

you will have to add arguments to the method call (between the ()).

To be clear, your file should look like

public class Converter {
    public static void main(String[] args) throws Exception{
      ....
    }

    public static double convertTemp(){
      ....
    }
}

of course, the meat of the code goes inside the method declarations.