Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package temperature;
- import java.util.Scanner;
- public class Temperature {
- public static void main(String[] args) {
- //MP 2 Temperature, a java program that prompts user for centigrade temperature and converts that number into Fahrenheit and Celcius
- //after 5 input numbers given from user, averages are printed to user
- //@ author Renee Waggoner
- Scanner userInput = new Scanner(System.in);
- double celsius, fahrenheit, averageFahrenheit, averageCentigrade, totalUserInput = 0, totalFahrenheit=0, totalCentigrade=0;
- //prompting user for input
- do {
- System.out.print("Enter Temp in Centigrade or <= -100.0 to quit: ");
- celsius = userInput.nextDouble();
- //user wants to continue, known by input>-100
- if(celsius>-100) {
- //calculations begin, this is plus one because initalized with zero
- totalUserInput++;
- //temperature conversion
- fahrenheit = (9.0/5.0)*celsius +32.0;
- //printing results to user with 1 decimal
- System.out.printf("Temperature: F(%.1f) C(%.1f)\n", fahrenheit,celsius);
- totalFahrenheit+=fahrenheit;
- totalCentigrade+=celsius;}
- }while(celsius>-100);
- if(totalUserInput>0) {
- //if statement true, compute averages
- averageFahrenheit = totalFahrenheit/totalUserInput;
- averageCentigrade = totalCentigrade/totalUserInput;
- }
- else {
- //if statement false, do this
- averageFahrenheit=0;
- averageCentigrade=0;
- }
- //printing averages to user with 1 decimal for centigrade and 15 decimals for fahrenheit
- System.out.printf("Average: Centigrade: (%.1f) ", averageCentigrade);
- System.out.printf("Average: Fahrenheit: (%.15f)", averageFahrenheit);
- userInput.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment