Java 使用变量 C 或 F 将摄氏度转换为华氏度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18971591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:59:23  来源:igfitidea点击:

Converting Celsius to Fahrenheit using variables C or F

javainput

提问by brl214

Im creating a program that will convert from Celsius to Fahrenheit and vice versa. I would like the user to input the temperature then C or F for which temperature it is. The issue I'm having is with the input being C or F. can you do if (input == 'F') then have it convert?

我正在创建一个程序,可以将摄氏度转换为华氏度,反之亦然。我希望用户输入温度,然后输入 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

回答by aBcDeFg123

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.

首先,你不能有“temps = (temps-32) * 5/9.0”,因为这就像说“50 = (50-32) * 5/9.0”它不起作用。您必须定义一个新的临时值并将其标记为这样。

回答by Srivastav

Hear is the code You are Looking For.

听到的是您正在寻找的代码。

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);
    }
}


And Output is.

输出是。

=== Converting Temperature ===

=== 转换温度 ===

Enter 1 for Fahrenheit to Celsius
Enter 2 for Celsius to Fahrenheit Something else to Exit.
Your Option:1

输入 1 表示华氏到摄氏度
输入 2 表示摄氏到华氏 其他要退出的东西。
您的选择:1

Enter a degree in Fahrenheit:200

输入华氏度:200

200.0 Fahrenheit is 93.33333333333334 celsius

200.0 华氏度是 93.33333333333334 摄氏度

Enter 1 for Fahrenheit to Celsius
Enter 2 for Celsius to Fahrenheit Something else to Exit.
Your Option:2

输入 1 表示华氏到摄氏度
输入 2 表示摄氏到华氏 其他要退出的东西。
您的选择:2

Enter a degree in Celsius:8754

输入摄氏度:8754

8754.0 celsius is 15789.2 Fahrenheit

8754.0 摄氏度是 15789.2 华氏度

Enter 1 for Fahrenheit to Celsius
Enter 2 for Celsius to Fahrenheit Something else to Exit.
Your Option:3

输入 1 表示华氏到摄氏度
输入 2 表示摄氏到华氏 其他要退出的东西。
您的选择:3

Bye..

再见..

:)

:)

回答by user4458695

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 +" oC = " + fahrenheit + " oF");
      }
 }