あなたが探しているコードを聞いてください。
package stackoverflow.java.Answers;
/*
Program for
http://stackoverflow.com/questions/18971591/converting-celsius-to-fahrenheit-using-variables-c-or-f
*/
import java.util.Scanner;
public class Temp_Convert{
public static void main(String[] args) {
// TODO Auto-generated method stub
print(" === Converting Temperature ===\n");
convertTemperature();
}
private static void convertTemperature() {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
print("\nEnter 1 for Fahrenheit to Celsius"
+ "\nEnter 2 for Celsius to Fahrenheit"
+ "\nSomething else to Exit." + "\nYour Option:");
int selection = input.nextInt();
if (selection == 1) {
print("Enter a degree in Fahrenheit:");
far2cel();
} else if (selection == 2) {
print("Enter a degree in Celsius:");
cel2far();
} else {
print("Bye..");
}
}
private static void cel2far() {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
print(celsius + " celsius is " + ((celsius * 9 / 5.0) + 32)
+ " Fahrenheit");
convertTemperature();
}
private static void far2cel() {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5 / 9.0))
+ " celsius");
convertTemperature();
}
private static void print(String string) {
// TODO Auto-generated method stub
System.out.print("\n" + string);
}
}
そしてアウトプットは。
===変換温度===
華氏から摂氏の場合は 1 を
入力します。摂氏から華氏の場合は 2 を入力します。
あなたのオプション:1
華氏で度を入力してください:200
200.0 華氏は 93.33333333333334 摂氏です
華氏から摂氏の場合は 1 を
入力します。摂氏から華氏の場合は 2 を入力します。
あなたのオプション:2
摂氏で度を入力してください:8754
8754.0 摂氏は 15789.2 華氏です
華氏から摂氏の場合は 1 を
入力します。摂氏から華氏の場合は 2 を入力します。
あなたのオプション:3
さよなら..
:)