Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TempConverter {
  4.  
  5. public static void main(String[] args) {
  6. boolean running = true;
  7.  
  8. while(running) {
  9.  
  10. Scanner sc = new Scanner(System.in);
  11. boolean understands = false;
  12.  
  13. // My menu
  14. System.out.println("Temperature Converter");
  15. System.out.println("====================================");
  16. System.out.println("");
  17. System.out.println("\t1) Fahrenheit to Celsius");
  18. System.out.println("\t2) Celsius to Fahrenheit");
  19. System.out.println("");
  20. System.out.println("====================================");
  21.  
  22. // The user's choice
  23. int input = sc.nextInt();
  24. String buff = sc.nextLine();
  25.  
  26. // If the user wants to convert F to C
  27. if(input == 1) {
  28. System.out.print("Please enter your temperature in Fahrenheit: ");
  29. double tempInF = sc.nextDouble();
  30. String buffer = sc.nextLine();
  31.  
  32. tempInF = (tempInF - 32) * 5/9.0;
  33. System.out.println("Your temperature in Celsius: " + tempInF);
  34.  
  35. // It just looks better with this
  36. try {
  37. Thread.sleep(1200);
  38. } catch(InterruptedException e) {
  39. e.getStackTrace();
  40. }
  41. }
  42.  
  43. // If the user wants to convert from C to F
  44. if(input == 2) {
  45. System.out.print("Please enter your temperature in Celsius: ");
  46. double tempInC = sc.nextDouble();
  47. String buffer = sc.nextLine();
  48.  
  49. tempInC = (tempInC * 9.0/5) + 32;
  50. System.out.println("Your temperature in Fahrenheit: " + tempInC);
  51.  
  52. // Again, it just looks better
  53. try {
  54. Thread.sleep(1200);
  55. } catch(InterruptedException e) {
  56. e.getStackTrace();
  57. }
  58. }
  59.  
  60. // If the user tryna be cool and not enter in one of the options
  61. if(input != 1 && input != 2) {
  62. System.out.println("That is not an option! Please choose either option 1 or 2!");
  63. }
  64.  
  65. // If the user puts in something nonsensical, then the computer will re-ask him or her
  66. while(!understands) {
  67. System.out.println("Do you want to input another temperature?");
  68. String yOrN = sc.nextLine();
  69.  
  70. if(yOrN.equals("y") || yOrN.equals("yes") || yOrN.equals("Y") || yOrN.equals("YES") || yOrN.equals("Yes")) {
  71. System.out.println("");
  72. understands = true;
  73. }
  74. else if(yOrN.equals("n") || yOrN.equals("no") || yOrN.equals("N") || yOrN.equals("NO") || yOrN.equals("No")) {
  75. System.out.println("Program is closing. Thank you for using Temperature Calculator.");
  76. running = false;
  77. understands = true;
  78.  
  79. sc.close();
  80. }
  81. else {
  82. System.out.println("Sorry, I didn't understand that.");
  83. System.out.println("Please type what you mean in a different way.");
  84. }
  85. }
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement