//Fahrenheit temperature and Celsius temperature conversion, write a program to convert Fahrenheit temperature to Celsius temperature or Celsius temperature conversion Chenghua temperature.
When the program is run, the temperature value and unit are read from the console, and another temperature value and unit is output, for example: If the user enters C
Program output 81F; If the user enters A. f, the program outputs a C (where C is the Celsius temperature, F is the Fahrenheit temperature), and the Fahrenheit temperature and the Celsius temperature conversion formula are:
C = 5 (F-32)/9
F = 9 C/5 + 32
The temperature value required for the input is an integer, and the output temperature value is also the rounded integer.
Package The third day _ exercises; Import Java.util.scanner;public class Test1 {public static void main (string[] args) {Scanner sc = new Scan NER (system.in); System.out.println ("Please enter a temperature"); String str = Sc.nextline ();//Read a string, do not write Sc.next (), although let is also read the string, but it is only a space as a delimiter Str=str.trim (). toUpperCase ();/ Remove the front and back spaces and full capitalization if (Str!=null && str.indexof ("C")!=-1)//actually str!=null can not write, because the above code shows that STR is not empty! {//Celsius to Fahrenheit string numstr=str.substring (0,str.indexof ("C")). Trim ();//For example, 7 CCD, structure is: 7 Cint num=integer.parseint (NUMSTR); The result is 7double doub=9*num/5.0+32;int dd= (int) (doub+0.5);//A more elegant way, such as 3.4+0.5 rounding or 3,3.5+0.5 rounding, is 4int dd2= (int) doub+ (( int) (DOUB*10)%10>5?1:0);//First Take integer to multiply this number by 10 to remove the single digit greater than 5 (with three mesh operator) System.out.println (num+ "c Celsius (c) to Fahrenheit (f) The result is" +dd+ "F") ;} else if (str!=null && str.indexof ("F")!=-1) {String numstr=str.substring (0,str.indexof ("F")). Trim (); int num= Integer.parseint (NUMSTR);d ouble doub=5* (num-32)/9.0;int dd= (int) (doub+0.5); int dd2= (int) doub+ (int) (DOUB*10)%10 >5?1:0; System.out.println (num+ "Fahrenheit (F) Celsius (c) After the result is" +dd+ "C");}}
Convert Fahrenheit temperature to Celsius or Celsius to Chenghua temperature. (Consider a variety of unlawful circumstances as far as possible to be legal)