2016-11-17 47 views
0

我是新來的Java,我想寫攝氏轉換到華氏轉換溫度爲華氏使用3個功能

import java.util.Scanner; 

public class Temps 
{ 

    public static void main(String[] args) 
    { 

     System.out.print("Enter temp(70 f or 20 c): "); 
     double temp = keyboard.nextDouble(); 
     String units = keyboard.next(); 
     if (units.equal("f")) 
     newtemp = ftoc(temp); 
     else if (units.equals("c")) 
     newtemp = ctof(temp); 
     else System.err.println("units must be c or f"); 
    } 

      public static double ftoc (int c) 
      return ((5.0/9.0) * (c - 32)); 
     } 

     public static double ctof (int f) 
     { 
      return ((9.0/5.0)* f+32); 
     } 
    } 

有人可以給我解釋一下我做錯了什麼程序。

+0

究竟是什麼問題?變量newtemp在哪裏聲明,爲什麼你不用計算結果做些什麼? –

+0

令人困惑的是,對於華氏溫度使用名爲'c'的變量,而對於攝氏溫度使用名爲'f'的變量。 –

回答

1

不少問題

看到在固定的代碼中的註釋

Scanner keyboard = new Scanner(System.in); 
    System.out.print("Enter temp(70 f or 20 c): "); 
     double temp = keyboard.nextDouble(); 
     String units = keyboard.next(); 
     double newtemp = -1; // not declared 
     if (units.equals("f")) // should be equals 
     newtemp = ftoc(temp); 
     else if (units.equals("c")) 
     newtemp = ctof(temp); 
     else System.err.println("units must be c or f"); 

     System.out.println("the new temp is " + newtemp); // need to print it out 
    } 

     public static double ftoc (double c) { // take a double 
      return ((5.0/9.0) * (c - 32)); 
     } 

     public static double ctof (double f) // take a double 
     { 
      return ((9.0/5.0)* f+32); 
     } 
相關問題