Method calling error [closed]

对着背影说爱祢 提交于 2019-12-14 00:07:52

问题


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.



来源:https://stackoverflow.com/questions/9771136/method-calling-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!