Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. package tw.luke.myproject.homework;
  2. import java.util.Scanner;
  3. /*題目:
  4. 宣告兩個變數,溫度和index
  5. 給使用者輸入溫度及index
  6. 當index=1時,°C->°F
  7. 當index=2時,°F->°C
  8. */
  9. public class HwTtestEx1 {
  10. public static void main(String[] args)
  11. {
  12. Scanner scn = new Scanner(System.in);
  13. System.out.println("==========The is Celsius and Fahrenheit conversion program==========");//讓使用者瞭解這是攝氏和華氏的換算程式
  14. System.out.print("Please input Number ");
  15. System.out.print("(input 1 is Celsius or 2 is Fahrenheits):");
  16. int index = scn.nextInt();//讓使用者自行輸入 1 or 2的值,輸入1=攝氏溫度、輸入2=攝氏溫度
  17. double temperature =0;//宣告一個溫度的變數
  18. if (index == 1)//如果使用者輸入1就會進入攝氏轉換華氏的程式碼
  19. {
  20. System.out.print("Please input Celsius temperature:");
  21. temperature = scn.nextDouble();//讓使用者自行輸入攝氏溫度
  22. double Fahrenheit = (temperature * 9 / 5 + 32);//宣告一個華氏變數,讓它代入攝氏換華氏公式。
  23. System.out.println("Now Celsius is " + temperature + "°C" + " Conversion Fahrenheit is " + Fahrenheit + "°F");//印出使用者輸入的攝氏溫度,及轉換成華氏溫度。
  24. } else if (index == 2)//如果使用者輸入2就會進入華氏轉換攝氏的程式碼
  25. {
  26. System.out.print("Please input Fahrenheits temperature:");
  27. temperature = scn.nextDouble();//讓使用者自行輸入華氏溫度
  28. double Celsius = ((temperature - 32) * 5 / 9);//宣告一個攝氏變數,讓它代入華氏換攝氏公式。
  29. System.out.println("Now Fahrenheit is " + temperature + "°F" + " Conversion Celsius is " + Celsius + "°C");//印出使用者輸入的華氏溫度,及轉換成攝氏溫度。
  30. } else//如果使用者輸入非1或2的數字就會執行底下程式碼
  31. {
  32. System.out.println("Input Error Number!!!");
  33. }
  34. System.out.println("Finish");
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement