【问题标题】:class, interface, or enum expected java compile error类、接口或枚举预期的 java 编译错误
【发布时间】:2015-01-13 18:54:04
【问题描述】:

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

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;
 }

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

【问题讨论】:

    标签: class methods interface call


    【解决方案1】:

    您过早地关闭了您的类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;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-09
      • 1970-01-01
      • 2014-09-08
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 2012-09-09
      相关资源
      最近更新 更多