Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. public class Problem4 {
  2.  
  3. public static double celsiusToFahrenheit(double degree_C)
  4. {
  5. double fahrenheitResult = (9*(degree_C) / 5) + 32;
  6. return fahrenheitResult;
  7. }
  8.  
  9. public static double fahrenheitToCelsius(double degree_F)
  10. {
  11. double celsiusResult = 5*(degree_F - 32)/9;
  12. return celsiusResult;
  13. }
  14.  
  15. public static void init(){
  16. Scanner scan = new Scanner(System.in);
  17. double result;
  18. char cont= 'y';
  19. char type;
  20.  
  21.  
  22. while (cont!='q' && cont!='Q')
  23. {
  24. System.out.println("Please enter the degree:");
  25. double degree = scan.nextDouble();
  26.  
  27. System.out.println("Now enter the type of conversion (For Celsius, type 'C'. For Fahrenheit, type 'F')");
  28. type = scan.next().charAt(0);
  29.  
  30. if(type!='c' && type!='C' && type!='f' && type!='F')
  31. {
  32. System.out.println("Error! You've entered an invalid conversion type!");
  33. System.out.println("Please enter a valid selection (Type C for celsius and F for fahrenheit");
  34. type = scan.next().charAt(0);
  35. }
  36. if (type == 'F' || type =='f')
  37. {
  38. result = fahrenheitToCelsius(degree);
  39. System.out.println("The conversion of "+ degree+"°F "+ "to Celsius is: " + result + "°C");
  40.  
  41. }
  42. if (type == 'C' || type =='c')
  43. {
  44. result = celsiusToFahrenheit(degree);
  45. System.out.println("The conversion of "+ degree+"°C "+ "to Fahrenheit is: " + result + "°F");
  46. }
  47.  
  48.  
  49. System.out.println("Would you like to quit? Type 'q' to quit or type any other letter to continue:");
  50. cont = scan.next().charAt(0);
  51. }
  52. scan.close();
  53. }
  54.  
  55. public static void main(String[] args)
  56. {
  57. init();
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement