-4

摂氏から華氏に、またはその逆に変換するプログラムを作成しています。ユーザーに温度を入力してから、CまたはFの温度を入力してもらいたいと思います。私が抱えている問題は、入力が C または F であることです。if (input == 'F') を変換できますか?

 import java.util.Scanner;

public class Test {
    public static void main(String [] args) 
    { 
     Scanner scan = new Scanner(System.in); 
String tempScale = ""; 

System.out.print("Enter the current outside temperature: "); 
double temps = scan.nextDouble(); 

System.out.println("Celsius or Fahrenheight (C or F): "); 
int input = scan.nextInt(); 

if (input == F) { 
// convert F to C 
temps = (temps-32) * 5/9.0; 
tempScale = "Celsius."; 
} else if (input == 'C') { 
// convert C to F 
temps = (temps * 9/5.0) +32; 
tempScale = "Fahrenheit."; 
}//end if/else 

System.out.println("The answer = "+temps+" degrees "+tempScale); 

}//end main() 
}//end class
4

3 に答える 3

1

あなたが探しているコードを聞いてください。

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

さよなら..

:)

于 2013-12-05T17:23:22.740 に答える