方法调用错误 [英] Method calling error

查看:54
本文介绍了方法调用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的程序中使用两种方法来转换温度我在调用我的方法时遇到问题这是我的代码:

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");

}
}
}

推荐答案

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

不是有效的 Java.你需要在main方法之外创建一个方法

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.

这篇关于方法调用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆