2014-02-24 247 views
0

我的任務是製作一個簡單的攝氏溫度或華氏度轉換爲攝氏溫度。我的實驗室老師對我有很大的幫助,但在解釋事情方面做得不好。我知道它的大部分功能以及它如何交互,並且代碼有0個錯誤,但沒有正確執行轉換。我會發布代碼,並且會//對我不理解的部分發表評論,但是我真正需要的是讓某人解釋我不理解的部分,並且修復腳本以使它更好正確執行。我會評論我不瞭解的部分。使用3種方法將攝氏溫度轉換成華氏度和華氏度轉換爲攝氏溫度

import java.util.Scanner; 

public class ExerciseOne 
{ 
public static void main(String[] args) 
{ 

    int choice; 


    System.out.println("What would you like to do? \n 1 - Fahnrenheit to Celsius \n 2 - Celsius to Fahrenheit \n 3 - Quit"); 
    Scanner dylan = new Scanner(System.in); 
    choice = dylan.nextInt(); 



    if (choice == 1) 
    { 
     System.out.println("What number do you want to convert to Celsius?"); 
     float input = dylan.nextFloat(); 
     float output = ftoC(input);  // I'm kind of confused as to why she told me to put the variable inside of the();  
     System.out.println(ftoC(input)); //Same here with the variables :(


    } 

    if (choice == 2) 
    { 
     System.out.println("What number do you want to convert to Fahrenheit?"); 
     float input = dylan.nextFloat(); 
     float output = ctoF(input); 
     System.out.println(ctoF(output)); 
    } 

    if (choice == 3) 
    { 
     System.out.println("Exiting application."); 
    } 

} 

public static float ftoC(float f) //This is a method line, but why is float f inside the parenthesis? 
{ 
    float celsius = (f-32)*(5/9); 
    return celsius; 
} 

public static float ctoF(float c) //Same thing goes for here 
{ 
    float fahrenheit = (c)*(9/5) + 32; 
    return fahrenheit; 
} 

}

+0

你需要讓'5/9'變成一個浮動或者雙重形式來實現「真正的分割」,就像你在學校學到的一樣。否則,Java將刪除剩下的部分(又稱分數)。有時候,這個數字會變爲0. – Brian

回答

1
float output = ftoC(input);  // I'm kind of confused as to why she told me to put the variable inside of the(); 

這樣,您可將變量傳遞給函數ftoC

System.out.println(ftoC(input)); //Same here with the variables :(

同上。數據如何轉到函數或方法?

public static float ftoC(float f) //This is a method line, but why is float f inside the parenthesis?

因爲你要告訴程序你的數據類型是什麼,所以它知道如何打好它在內存中。否則,它不知道你想要你的數據類型在內存中有多大。另外,如果您要將int傳遞給期望float的方法,則該程序將顯示不同的行爲。

+0

謝謝GI Joe,你對解決我的問題非常有幫助。我有一個最後一個問題,我完成了ExerciseOne,第二個練習要求:在main中添加一行,調用ftoC方法,並將步驟2中聲明的變量作爲參數傳遞。 我設置了double var = 0; 我想知道如何調用該方法。這與主要方法中的內容相似嗎? double var = 0; \t \t public static float ftoC(double var); – user3304333

+0

假設你以後將'var'設置爲'0'以外的東西,那麼是的。如果你不能弄明白,你應該問另一個問題。但是我認爲你需要在一本關於Java的書中做一些研究和閱讀。 – Brian

+0

那麼練習告訴我這個: 在主要方法聲明一個變量的類型 雙 並初始化爲 一個值。 3.在main中添加一行,調用 fToC 方法,並將 參數作爲參數傳遞給第2步中聲明的變量。 4.保存並嘗試編譯該程序。 Don ' 不用擔心添加 ing 或 提交 到Git存儲庫 。這個練習不會被分級。 5. 當你嘗試編譯你應該得到以下錯誤消息: > TempConverter.java:16:FTOC(浮動)在TempConverter不能應用於 > 到(雙) > 浮動溫度= FTOC(測試); > ^ > 1錯誤 – user3304333

1

對於初學者來說,這並不做你所期望的:

(f-32)*(5/9); 

5/9作爲整數除法進行的,所以它等同於0任何乘以0也是0 您需要鑄5和/或9漂浮:

(f-32)*(5f/9f); 
+0

解決方案實際上比這個簡單:) – Bohemian

+0

這解決了華氏溫度到攝氏度問題!但由於某種奇怪的原因,攝氏溫度到華氏溫度的公式計算不正確。華氏浮標=(9/5)* c)+32; (9/5)* 20 = 36,36 + 32 = 68,但由於某種原因,我得到了84. – user3304333

+1

@ user3304333:刪除括號中的括號'9/5' ...實際上刪除所有括號。你不需要它們。 – Brian

2

在時間以一個問題...

float output = ftoC(input); // I'm kind of confused as to why she told me to 

把變量放在()中;

這裏ftoC是一種方法和input被傳遞給它

System.out.println(ftoC(input)); //Same here with the variables :(

您所呼叫的方法再次。無意義。只是從以前的調用打印結果:

System.out.println(output); 

此外,我會改變這些行:

float input = dylan.nextFloat(); 
float output = ftoC(input); 
System.out.println(output); 

要剛:

System.out.println(ftoC(dylan.nextFloat())); 

您不需要這些局部變量,只需將每個返回值直接傳遞給下一個方法即可。


public static float ftoC(float f) //This is a method line, but why is float f inside the parenthesis? 

參數(如果有的話)在括號中聲明的,作爲一種類型的後跟一個參數名稱。在這裏,我們有一個float參數調用f


但也有幾個錯誤。在java中,整數除法得到整數結果,這意味着截斷非整數部分,所以5/9int0。但要解決這個問題,只是去掉括號!:

public static float ftoC(float f) { 
    return (f-32)*5/9; 
} 

通過去掉括號,這個問題是因爲在Java固定,如果操作數一個float和其他int,結果是float。該計算首先從float減去int - 給出float。然後乘以int5 - 給出float。然後再除以int9 - 給出float

請注意,我沒有打擾局部變量 - 只是返回計算。代碼少就好。

+0

最後一部分是完全合乎邏輯的,只是很多人不這麼想。把它放在紙上,你明白爲什麼這是正確的。 – Brian

相關問題