class,interface或enum期望的java编译错误

时间:2014-11-16 22:39:58

标签: class methods interface call

我的任务是编写显示临时表的代码和将华氏温度转换为摄氏温度的方法。到目前为止这是我的代码。我似乎无法弄清楚什么是错的

import java.text.DecimalFormat;
import java.util.Scanner;

public class temperature
{
    public static void main(String[] args)
    {
            double fah;
            int choice;
            Scanner keyboard = new Scanner(System.in);
            DecimalFormat formatter = new DecimalFormat("#.0");
            do
            {
                    tempTable();
                    System.out.println("Enter degrees in Fahrenheit");
                    System.out.println("and I will convert it to Celsius");
                    fah = keyboard.nextDouble();

                    convert(fah);

                    System.out.println(formatter.format(fah) + " degrees Fahrenheit is        " + formatter.format(cel) + " degrees Celsius");
                    System.out.println("Press 1 to continue");
                    System.out.println("Press 2 to exit");
                    choice = keyboard.nextInt();
            }while(choice!=2);
    }
}


/**
   tempTable displays the fahrenheit to celsius temperature table
*/

public static void tempTable()
{
    System.out.println("Celsius Temperature Table\n");
    System.out.println("Fahrenheit\tCelsius\n");
    System.out.printf("%3.1f\n", 0.0);
    System.out.printf("%3.1f\n", 1.0);
    System.out.printf("%3.1f\n", 2.0);
    System.out.printf("%3.1f\n", 3.0);
    System.out.printf("%3.1f\n", 4.0);
    System.out.printf("%3.1f\n", 5.0);
    System.out.printf("%3.1f\n", 6.0);
    System.out.printf("%3.1f\n", 7.0);
    System.out.printf("%3.1f\n", 8.0);
    System.out.printf("%3.1f\n", 9.0);
    System.out.printf("%3.1f\n", 10.0);
    System.out.printf("%3.1f\n", 11.0);
    System.out.printf("%3.1f\n", 12.0);
    System.out.printf("%3.1f\n", 13.0);
    System.out.printf("%3.1f\n", 14.0);
    System.out.printf("%3.1f\n", 15.0);
    System.out.printf("%3.1f\n", 16.0);
    System.out.printf("%3.1f\n", 17.0);
    System.out.printf("%3.1f\n", 18.0);
    System.out.printf("%3.1f\n", 19.0);
    System.out.printf("%3.1f\n\n", 20.0);
}

/**
   celsius returns degrees in fahrenheit
   @param fah degrees in fahrenheit enter by user
   @return returns degrees in celsius
*/

 public static double convert(double fah)
 {
      double cel;

      cel = (fah-32)5/9;

      return cel;
 }

当我通过终端编译代码时,我得到了类,接口,枚举预期错误。 我不知道我做错了什么。这不是怎么写方法的吗?

1 个答案:

答案 0 :(得分:0)

您过早地关闭了您的类temperature(应该是Java命名约定Temperature)。此外,您的convert方法使用整数除法,您错过了乘法操作数。

public static void main(String[] args) {
    double fah;
    int choice;
    Scanner keyboard = new Scanner(System.in);
    DecimalFormat formatter = new DecimalFormat("#.0");
    do {
        tempTable();
        System.out.println("Enter degrees in Fahrenheit");
        System.out.println("and I will convert it to Celsius");
        fah = keyboard.nextDouble();

        double cel = convert(fah); // <-- declare cel.
        System.out.println(formatter.format(fah)
                + " degrees Fahrenheit is        " + formatter.format(cel)
                + " degrees Celsius");
        System.out.println("Press 1 to continue");
        System.out.println("Press 2 to exit");
        choice = keyboard.nextInt();
    } while (choice != 2);
} // <-- only one brace after while, the other methods should be in the class.

/**
 * celsius returns degrees in fahrenheit
 * 
 * @param fah
 *            degrees in fahrenheit enter by user
 * @return returns degrees in celsius
 */

public static double convert(double fah) {
    double cel = (fah - 32) * ((double) 5 / 9); /* <-- multiply. 
                                                       not integer math. */
    return cel;
}