-4

我正在创建一个程序,它将从摄氏温度转换为华氏温度,反之亦然。我希望用户输入温度,然后输入 C 或 F 的温度。我遇到的问题是输入是 C 或 F。如果 (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

2
import java.util.Scanner;
 public class JavaApplication {
      public static void main(String[] args) {
           Scanner sc = new Scanner(System.in);
           double celsius, fahrenheit;
           System.out.print("Enter a temperature in Celsius: ");
           celsius = sc.nextDouble();
           fahrenheit = 32 + (celsius * 9 / 5);
           System.out.println(celsius +" ºC = " + fahrenheit + " ºF");
      }
 }
于 2015-01-15T17:37:26.667 回答
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 回答
-3

Well first of all, you can't have "temps = (temps-32) * 5/9.0" because that's like saying "50 = (50-32) * 5/9.0" it doesn't work. You have to define a NEW temp and label it as such.

于 2013-10-06T03:32:30.530 回答