2014-11-16 2 views
0

моим назначением было написать код, отображающий таблицу temp и метод преобразования по Фаренгейту в цель. Это мой код до сих пор. Я не могу показаться, чтобы выяснить, что случилоськласс, интерфейс или enum ожидаемая ошибка компиляции java

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

Я получаю класс, интерфейс, перечисление ожидается ошибку, когда я компиляции кода через терминал. Я понятия не имею, что я делаю неправильно. Не так ли писать методы?

ответ

0

Вы преждевременно закрыли свой класс temperature (это должно было быть Temperature по соглашению о присвоении имен Java). Кроме того, ваш метод 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; 
} 
Смежные вопросы