![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Temperature Conversion Program - Help!
I am soooooo lost!
Here's the assignment: "Write a program that allows the user to convert either from degrees Celsius to Fahrenheit or from degrees Fahrenheit to Celsius. In a loop, prompt the user to enter a temperature (as an integer). In a nested loop, prompt the user to enter a letter for Celsius or Fahrenheit. Use a switch statement to analyze the user input and to request to reenter the letter (but not the temperature) in case of invalid input. Print the result of conversion as a double and prompt the user to choose to continue or to quit." More details from the book assignment: "Write a program that allows the user to convert either from degrees Celsius to Fahrenheit or from degrees Fahrenheit to Celsius. Use the following formulas: degreesC = 5(degreesF = 32)/9 degreesF = (9(degreesC)/5) + 32 Prompt the user to enter a temperature and either a 'C' (or 'c') for Celsius or an 'F' (or 'f') for Fahrenheit; allow either uppercase or lowercase, but if anything other than 'C', 'c', 'F', or 'f' is entered, print an error message and ask the user to reeneter a valid selection (uppercase or lowercase 'C' or 'F'). Convert the temperature to Fahrenheit if Celsius is entered, or to Celsius if Fahrenheit is entered, and then ask the user to press 'Q' or 'q' to quit or to press any other key to repeat the loop and perform another conversion."
import java.util.*;
public class TempConversion
{
public static void main(String[] args)
{
int degreesC, degreesF;
System.out.println("=====TEMPERATURE CONVERSION=====");
System.out.println();
System.out.println("Please enter a whole number for");
System.out.println("conversion:");
Scanner keyboard = new Scanner(System.in);
degreesC = keyboard.nextInt();
degreesF = keyboard.nextInt();
}
}This is how far I am. Not far at all, in other words. I desperately need help on this one ![]() Thanks |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
I don't understand why it says "in a loop, prompt the user to enter a temperature (as an integer)"... isn't what I've come up with above perfectly sufficient for this first user-input?
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 28
Rep Power: 0
![]() |
You have no loops in your code. That is your first problem. There are different types of loops, i.e. for while do-while etc... Try those and see what you come up with.
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Working on it. I just don't understand why I'm supposed to use a loop just so the user can enter an integer..
![]() |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
also ask the user which conversion they require?
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#6 |
|
Programmer
|
are you allowed to use JOptionPane ? if yes then:
import java.util.*;
public class TempConversion
{
public static void main(String[] args)
{
double result;
String degree;
int input;
degree = JOptionPane.showInputDialog("Enter: \n C - Fahrenheit to Celsius\n F - Celsius to Fahrenheit");
input = new Integer(JOptionPane.showInputDialog("Enter the degree:")).intValue();
if(degree == "C" || degree == "c")
{
result = 5.0/9.0 * (input - 32);
System.out.println(input + " Fahrenheit is " + result + " Celsius");
}
else if(degree == "F" || degree == "f")
{
result = 9.0/5.0 * input + 32;
System.out.println(input + " Celsius is " + result + " Fahrenheit");
}
}
}
__________________
countdown++; |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
No.. we are not doing Swing at all. But thanks.. that helped somewhat
![]() |
|
|
|
|
|
#8 | |
|
Newbie
Join Date: Jun 2005
Posts: 28
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#9 | |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Quote:
And this is without Swing, so no JOptionPane |
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Jun 2005
Posts: 28
Rep Power: 0
![]() |
The only reason I could think of why they have (2) variables is that, if you want to use the degreeF later on and you initially converted to degreeC, you just have to use the degreeF variable (which means you don't have to reconvert it again). So, I would stick this in the beginning:
// if they input it in farenheit first degreesC = 5.0/9.0 * (input - 32); degreesF = input; //if they input it in celsius first degreesF = 9.0/5.0 * input + 32; degreesC = input; That way, if you need the temperature in Farenheit, you could just use the variable degreesF and vice versa |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|