letsdoitjava

Temperature

Jun 9th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. package temperature;
  2. import java.util.Scanner;
  3.  
  4. public class Temperature {
  5.     public static void main(String[] args) {
  6.         //MP 2 Temperature, a java program that prompts user for centigrade temperature and converts that number into Fahrenheit and Celcius
  7.         //after 5 input numbers given from user, averages are printed to user
  8.  
  9.         //@ author Renee Waggoner
  10.  
  11.         Scanner userInput = new Scanner(System.in);
  12.         double celsius, fahrenheit, averageFahrenheit, averageCentigrade, totalUserInput = 0, totalFahrenheit=0, totalCentigrade=0;
  13.  
  14.         //prompting user for input
  15.         do {
  16.             System.out.print("Enter Temp in Centigrade or <= -100.0 to quit: ");
  17.             celsius = userInput.nextDouble();
  18.  
  19.             //user wants to continue, known by input>-100
  20.             if(celsius>-100) {
  21.  
  22.                 //calculations begin, this is plus one because initalized with zero
  23.                 totalUserInput++;
  24.  
  25.                 //temperature conversion
  26.                 fahrenheit = (9.0/5.0)*celsius +32.0;
  27.  
  28.                 //printing results to user with 1 decimal
  29.                 System.out.printf("Temperature: F(%.1f) C(%.1f)\n", fahrenheit,celsius);
  30.                 totalFahrenheit+=fahrenheit;
  31.                 totalCentigrade+=celsius;}
  32.         }while(celsius>-100);
  33.  
  34.         if(totalUserInput>0) {
  35.             //if statement true, compute averages
  36.             averageFahrenheit = totalFahrenheit/totalUserInput;
  37.             averageCentigrade = totalCentigrade/totalUserInput;
  38.         }
  39.         else {
  40.             //if statement false, do this
  41.             averageFahrenheit=0;
  42.             averageCentigrade=0;
  43.         }
  44.         //printing averages to user with 1 decimal for centigrade and 15 decimals for fahrenheit
  45.         System.out.printf("Average: Centigrade: (%.1f) ", averageCentigrade);
  46.         System.out.printf("Average: Fahrenheit: (%.15f)\n", averageFahrenheit);
  47.         userInput.close();
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment