Celcius到Fahr使用方法但方法说未定义

时间:2016-10-06 01:23:49

标签: java

我需要帮助调用我的方法。它说"the method fahrtoCel" is undefined for the "TempConversion"

import java.util.Scanner;

public class TempConversion {

    public static void main(String[] args) {

        System.out.println("===CONVERTING TEMPERATURE=====");
        ConvertTemp();
    }

        private static void ConvertTemp(){
            Scanner s = new Scanner(System.in);
            System.out.println("Press 1: Convert Celcius to Fahrenheit\nPress 2: Convert Fahrenheit to Celcius\n");
            int select = s.nextInt();

            if (select==1){
                System.out.print("Enter the fahrenheit: "); 
                celtoFahr();
            }   else if (select==2){
                System.out.print("Enter the celcius: ");
                fahrtoCel();
            }   else{
                System.out.println("exit");

            }

            private static void celtoFahr(){
                double temperature = s.nextDouble();
                double celcius = 5.0/9.0*(temperature-32);

            }
            private static void fahrtoCel(){
                double temperature = s.nextDouble();
                double fahrenheit = 9.0/5.0*(temperature+32);
            }

    }

}

1 个答案:

答案 0 :(得分:0)

您错过了结束}

private static void ConvertTemp(){
    Scanner s = new Scanner(System.in);
    System.out.println("Press 1: Convert Celcius to Fahrenheit\nPress 2: Convert Fahrenheit to Celcius\n");
    int select = s.nextInt();

    if (select==1){
        System.out.print("Enter the fahrenheit: "); 
        celtoFahr();
    }   else if (select==2){
        System.out.print("Enter the celcius: ");
        fahrtoCel();
    }   else{
        System.out.println("exit");

    }

    // add closing curly
   }
    private static void celtoFahr(){
    ...

如果您这样做,s将超出范围,因此请更改您的方法以接受扫描程序。

e.g。

private static void ConvertTemp(){
    Scanner s = new Scanner(System.in);
     ....
        celtoFahr(s);
    ....
}

private static void celtoFahr(Scanner s){
     ....
}

如果使用像Eclipse这样的IDE,这个错误将立即显而易见