# Do While Loop - Converting Celsius to Fahrenheit and averaging

Page 1 of 1

## 5 Replies - 12429 Views - Last Post: 19 September 2010 - 07:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=190807&amp;s=ab86098b665a7f6ba06607eeeff7ba0c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Sally1217801999

Reputation: 0
• 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

Also:

***************************************
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: 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;
int totalUserInput = 0;

do
{
System.out.print("Enter Temp in Centigrade or <= -100.0 to quit: ");
double averageFahrenheit = totalFahrenheit/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

• Saucy!

Reputation: 6246
• Posts: 24,014
• 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.

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• 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.

### #4 bennitto

Reputation: 6
• 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:

- 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{
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

### #5 pbl

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

Reputation: 8378
• Posts: 31,956
• 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
}

```

### #6 Dogstopper

Reputation: 2965
• Posts: 11,222
• 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.