converttemperatures.java

来自「《A first book of java》by Gary J.Bronson 」· Java 代码 · 共 40 行

JAVA
40
字号
import java.io.*;   // needed for keyboard input
import java.text.*;  // needed for formatting
public class ConvertTemperatures
{
  public static void main(String[] args)
  throws java.io.IOException   // for keyboard input
  {
    int tempType;
    double temp, fahren, celsius;
    String s1;
      // set up format variable
    DecimalFormat num = new DecimalFormat(",###.00");
      // set up the basic input stream for keyboard entry
      // needed for conversion capabilities  
    InputStreamReader isr = new InputStreamReader(System.in);
      // needed to use ReadLine()
    BufferedReader br = new BufferedReader(isr);
    System.out.print("Enter the temperature to be converted: ");
    s1 = br.readLine();
    temp = Double.parseDouble(s1);
     
    System.out.println("Enter an 1 if the temperature is in Fahrenheit");
    System.out.print(" or a 2 if the temperature is in Celsius: ");
    s1 = br.readLine();    
    tempType = Integer.parseInt(s1);
    if (tempType == 1)
    {
      celsius = (5.0 / 9.0) * (temp - 32.0);
      System.out.println("\nThe equivalent Celsius temperature is "
                      + num.format(celsius));
    }
    else
    {
      fahren =  (9.0 / 5.0) * temp + 32.0;
      System.out.println("\nThe equivalent Fahrenheit temperature is "
                      + num.format(fahren));
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?