Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. /**
  2. *This code converts a temperature to Celsius or Fahrenheit
  3. *
  4. * @Molly Limaye
  5. * @09/05/19
  6. */
  7.  
  8. import java.util.*;
  9.  
  10. public class TempConverter {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. Scanner sc = new Scanner(System.in);
  15.  
  16. //play again loop
  17. int run = 1;
  18. while (run==1){
  19. //user input
  20. System.out.print("Enter a temperature: ");
  21. double temp = sc.nextInt();
  22.  
  23. System.out.println("Enter 1 to convert to Celsius.\nEnter 2 to convert to Fahrenheit.");
  24. int convert = sc.nextInt();
  25. String unit = "F";
  26. //conversions
  27. if (convert==1){
  28. temp = (temp - 32) * 5/9.0;
  29. unit = "C";
  30. }
  31. else {
  32. temp = ((9.0/5 * temp) + 32);
  33. }
  34. System.out.println("The temperature is " + temp + "ΒΊ" + unit + ".");
  35. //play again feature
  36. System.out.println("Would you like to convert another temperature? (1=yes/2=no)");
  37. int answer = sc.nextInt();
  38. if (answer==1){
  39. run = 1;
  40. }
  41. else{
  42. run = 0;
  43. }
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement