我的任務是製作一個簡單的攝氏溫度或華氏度轉換爲攝氏溫度。我的實驗室老師對我有很大的幫助,但在解釋事情方面做得不好。我知道它的大部分功能以及它如何交互,並且代碼有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;
}
}
你需要讓'5/9'變成一個浮動或者雙重形式來實現「真正的分割」,就像你在學校學到的一樣。否則,Java將刪除剩下的部分(又稱分數)。有時候,這個數字會變爲0. – Brian