5 Replies - 7931 Views - Last Post: 19 September 2010 - 07:35 PM Rate Topic: -----

#1 Sally1217801999  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-October 08

Do While Loop - Converting Celsius to Fahrenheit and averaging

Posted 17 September 2010 - 08:58 AM

I'm supposed to create a program that has a do while loop. Inside the while loop, your program will
prompt the user for temperature in Centigrade. If the Centigrade value read in is <= -100, the loop will exit. Otherwise, your program will compute the Fahrenheit equivalent temperature. Inside the while loop, your program will print out the Centigrade and Fahrenheit temperatures.

Keep a count of the valid user inputs and a running total of your Fahrenheit and Centigrade temperatures.

When the user terminates the while loop, print out the average Centigrade and Fahrenheit temperatures. Note that
Average_Fahrenheit = Total_of_Fahrenheit/ total_user_inputs
Average_Centigrade = Total_of_Centigrade / total_use_inputs

Also:

Fahrenheit = (9.0/5.0)*Centigrade + 32.0

***************************************
Here is some sample output from your program:
***************************************
Enter Temp in Centigrade or <= -100.0 to quit:
20
Temperature: F(68.0) C(20.0)
Enter Temp in Centigrade or <= -100.0 to quit:
30
Temperature: F(86.0) C(30.0)
Enter Temp in Centigrade or <= -100.0 to quit:
40
Temperature: F(104.0) C(40.0)
Enter Temp in Centigrade or <= -100.0 to quit:
1
Temperature: F(33.8) C(1.0)
Enter Temp in Centigrade or <= -100.0 to quit:
-10
Temperature: F(14.0) C(-10.0)
Enter Temp in Centigrade or <= -100.0 to quit:
-100
Average: Centigrade(16.2)
Average: Fahrenheit(61.160000000000004)


Here is what I have so far:

import java.util.Scanner;

public class TemperatureConversion {

	public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in);
		int totalFahrenheit = 0;
		int totalCelsius = 0;
		double AverageFahrenheit = 0;
		double AverageCentigrade = 0;
		int totalUserInput = 0;
                
                do
		{
			System.out.print("Enter Temp in Centigrade or <= -100.0 to quit: ");
			double averageFahrenheit = totalFahrenheit/totalUserInput;
			double averageCentigrade = totalCelsius/totalUserInput;
		
		}
		

	}

}


I'm having a hard time with the whole do while concept

MOD EDIT: When posting code...USE CODE TAGS!!!

Attached File(s)


This post has been edited by JackOfAllTrades: 17 September 2010 - 09:17 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Do While Loop - Converting Celsius to Fahrenheit and averaging

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Do While Loop - Converting Celsius to Fahrenheit and averaging

Posted 17 September 2010 - 09:45 AM

What don't you understand about do-while loops? They're always entered at least once, and the while condition is not evaluated until the end of the loop.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,259
  • Joined: 27-December 08

Re: Do While Loop - Converting Celsius to Fahrenheit and averaging

Posted 17 September 2010 - 09:51 AM

The basic syntax is:
do{
   //code
}while(condition);



You are missing a while statement at the end of your do loop. Also, what I would do in your loop is:
-prompt for input
-add the input to the running tally

Then average after the loop to save steps.
Was This Post Helpful? 0
  • +
  • -

#4 bennitto  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 64
  • Joined: 28-November 09

Re: Do While Loop - Converting Celsius to Fahrenheit and averaging

Posted 18 September 2010 - 06:09 PM

I am not an experienced programmer and may not have worked on any complex applications but I know the importance of planning, especially when you are stuck.

In your situation I would list the things I would need to do to solve this task:

- ask the user the temperature in centigrade
- convert to fahrenheit
- add to a running total of farenheit
- add to a running total of degrees
- add to a running total of conversions(or user inputs if you prefer)


I then know I would have to do this until the input is <= -100.0. So I know my program will have to do something until this condition is met.

macosxnerd101 correctly pointed out the syntax of the do-while loop.

do{
  //code
}while(condition)


The condition is what terminates the loop. So now you have:

do{
 ask the user the temperature in centigrade
 convert to fahrenheit
 add to a running total of farenheit
 add to a running total of degrees
 add to a running total of conversions(or user inputs if you prefer)
}while(input <= -100)


After this, you just have to divide the running totals of degrees & farenheit by the running total of user inputs and display it.

This post has been edited by bennitto: 18 September 2010 - 06:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Do While Loop - Converting Celsius to Fahrenheit and averaging

Posted 18 September 2010 - 09:20 PM

do while might not be the good construct for that

do while assume that the body of the loop will be executed and the test is performed after
better to use and infinite loop with a break statement

while(true) {
   double userInput = ....
   if(userInput <= -100.0)
      break;
   ... perform calculations
}


Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Do While Loop - Converting Celsius to Fahrenheit and averaging

Posted 19 September 2010 - 07:35 PM

If you read your assignment, it never mentions a do-while loop; it simply asks for a while loop. pbl is correct here because that is what your assignment is asking for.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1