Results 1 to 7 of 7
Thread: Loops and display problem
- 02-26-2010, 02:48 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 8
- Rep Power
- 0
Loops and display problem
Hey all,
so new java developer here and I am trying to create a program that will show this to the user.
Please enter the temperature you would like to convert - 0
Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2
0.00000 degrees Celsius = 32.000 degrees Fahrenheit.
Would you like to continue? ( 0 = No ; 1 = Yes ) - 1
Please enter the temperature you would like to convert – 32.0
Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 1
32.000 degrees Fahrenheit = 0.00000 degrees Celsius.
Would you like to continue? ( 0 = No ; 1 = Yes ) – 0.0
Good Bye!
I have hit some road blocks in my development and I was hoping someone could point me in the right direction.
As this is a school project, I would rather do it myself than have someone rewrite all my code. Thanks
Here is my code ( I havent modified the output to match the required output as I was just plugging things in first) Also for this I need celcius to have 5 decimal points and Fahrenheit to have 3)
mport java.util.Scanner;
public class Lab{
public static void main (String [] args){
Scanner input = new Scanner(System.in);
{
double celsius;
double farenheit;
int degrees;
System.out.print("Please enter the temperature you would like to convert : ");
double temp = input.nextDouble ();
char response = 0;
while (response == 'Y')
{
System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2");
degrees = input.nextInt ();
if((degrees == '1') || (degrees == '1'))
{
farenheit = 9/5 * (temp) + 32;
System.out.println("Temperature = " +temp + 'C');
System.out.println("Temperature in Farenheit = " +farenheit +'F');
}
else if ((degrees == 'F') || (degrees == 'f'))
{
celsius = 5/9 * (temp - 32);
System.out.println("Temperature = " +temp + '1');
System.out.println("Temperature in Celsius = " +celsius + '2');
}
else
{
System.out.println("Good Bye");
//System.out.println("You entered" +degrees);
System.out.println("Enter 1 for Celsius or 2 for Farenheit");
System.out.print("Enter temperature: ");
temp = input.nextDouble ();
}
System.out.print("Would you like to enter another temperature? Y or N: ");
//System.out.print("Enter <strong class="highlight">a</strong> temperature: ");
//temp = Input.readDouble ();
}
}
} // End Main
} // End Class DegreesConverter
- 02-26-2010, 03:21 PM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Hey, I 've ust tried to run your code and even I am stuck. Im not sure what is meant to happen. No matter what value you enter the program just quits. :confused:
Seems that you are having some confusion with your variables too: You ask the user for an input and its just "fluffy".what is the purpose of temp?Java Code:double temp = input.nextDouble ();
I think the main problem is that you use three variables. why dont you just ask the user to select 1 for F and 0 for C.Then depending of the selection enter temperature in F and convert it to C and vice versa and get rid of variable "degrees".
This looks interesting too:
Seems like your code expects the user to enter "1" or "F" :confused: while you asked the user to input 1 or 2. It may be just a typo. I dont know. Plus if you declare a char variable, java understands the value as ASCII character - so it converts it to a decimal value(sort off). I guess it would be easier if you declare it as String instead of char.Java Code:System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2"); degrees = input.nextInt (); if((degrees == '1') || (degrees == '1')) { farenheit = 9/5 * (temp) + 32; System.out.println("Temperature = " +temp + 'C'); System.out.println("Temperature in Farenheit = " +farenheit +'F'); } else if ((degrees == 'F') || (degrees == 'f')) { celsius = 5/9 * (temp - 32); System.out.println("Temperature = " +temp + '1'); System.out.println("Temperature in Celsius = " +celsius + '2'); }
Seems like the code is too complicated and "fluffy" for the intended purpose. If you have some sort of specification that you are following that would be interesting to see too.Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 02-26-2010, 03:33 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 8
- Rep Power
- 0
Thanks for your help,
I too am having trouble running the program. I made some changes as best I know how based on your reply, but the program will terminate after the first line.
What I am trying to build is a program that will ask the user for a temp
Store that
Ask the user to input 1 for Fahrenheit and 2 for Celsius
(Use five decimal places for Fahrenheight and three for Celsius)
Calculate the temp based on user input
Display User input and calculation (32.000 degrees Fahrenheit = 0.00000 degrees Celsius.)
Ask the user if they want to calculate another temp (Y or N)
Y restarts the program while N ends with good bye
Here are my modifications, any help would be greatly appreciated as I am beyond lost now.
import java.util.Scanner;
public class Lab4{
public static void main (String [] args){
Scanner input = new Scanner(System.in);
{
double celsius;
double farenheit;
int degrees;
System.out.print("Please enter the temperature you would like to convert : ");
double temp = input.nextDouble ();
char response = 0;
while (response == 'Y')
{
System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2");
degrees = input.nextInt ();
if((degrees == '1') || (degrees == '1'))
{
farenheit = 9/5 * (temp) + 32;
System.out.println("Temperature = " +temp + '1');
System.out.println("Temperature in Farenheit = " +farenheit +'1');
}
else if ((degrees == '2') || (degrees == '2'))
{
celsius = 5/9 * (temp - 32);
System.out.println("Temperature = " +temp + '1');
System.out.println("Temperature in Celsius = " +celsius + '2');
}
else
{
System.out.println("Enter 1 for Celsius or 2 for Farenheit");
System.out.print("Enter temperature: ");
temp = input.nextDouble ();
}
System.out.print("Would you like to enter another temperature? Y or N: ");
if (response == 'N')
System.out.println("Good Bye");
//System.out.println("Good Bye);
//System.out.print("Enter <strong class="highlight">a</strong> temperature: ");
//temp = Input.readDouble ();
}
}
} // End Main
} // End Class DegreesConverter
- 02-26-2010, 03:46 PM #4
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
<variable name> = ScannerInput; <-- you automatically store it in a <variable name>What I am trying to build is a program that will ask the user for a temp
ok so this time you need a different <variable name1> to store 1 or 2. I guess int would do. What do you think? So its a simple if statement.Ask the user to input 1 for Fahrenheit and 2 for Celsius
If <variable name1> == something
{
DO THIS and display output
}
else if <variable name1> == something
{
DO THIS and display output
}
This will require a loop. So the idea is that: while <variable name> is Y run the code. Hint: loop will have to cover the whole code (if you know what i mean).Ask the user if they want to calculate another temp (Y or N)Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 02-26-2010, 04:02 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 8
- Rep Power
- 0
First of all, thank you so much for your help.
Secondly,
Is this a good start to looping the whole thing together?
import java.util.Scanner;
public class Lab4{
public static void main (String [] args){
Scanner input = new Scanner(System.in);
{
char Result;
int Degrees;
int decision;
if decision ==1{
System.out.print("Good Bye")
else decision == 0{
System.out.print("Please enter the temperature you would like to convert - ");
Temp = input.nextDouble();
- 02-26-2010, 04:13 PM #6
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Well you havent looped anything yet. If statements do not loop. Read about While, Do Until, Do While, For loops.
Java Code:import java.util.Scanner; public class Lab4{ public static void main (String [] args) { <someVariable> = 1; Scanner input = new Scanner(System.in); <someVariable = input; while{<someVariable>==1) { //run your code }Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 02-26-2010, 04:26 PM #7
Member
- Join Date
- Feb 2010
- Posts
- 8
- Rep Power
- 0
Again thank you so much for all of your help
I have reread the areas on loops, and I know I have some issues here, but any chance you could tell me what is wrong here?
i rewrote based on my reading and your suggestions
import java.util.Scanner;
public class Lab4{
private static double farenheit;
private static double celsius;
public static void main (String [] args){
Scanner input = new Scanner(System.in);
{
int Degrees;
int decision = 0;
double Temp;
System.out.print("Please enter the temperature you would like to convert - ");
Temp = input.nextDouble();
System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2");
Degrees = input.nextInt ();
if((Degrees == '1') || (Degrees == '1')){{
farenheit = 9/5 * (Temp) + 32;
System.out.println("(Temp) + Degrees Celsius = (farenheight) + degrees Farenheight");
System.out.print("Would you like to continue? ( 0 = No ; 1 = Yes ) – 0.0");}}
else if(Degrees =='0');
celsius = 5/9 * (Temp - 32);
System.out.println("(Temp) degrees Farenheight = (celsius) Degrees Celsius");}
System.out.print("Would you like to continue? ( 0 = No ; 1 = Yes ) – 0.0");
int decision = (int) input.nextDouble();}
{
char decision = 0;
while (decision =='1'){
System.out.print("Good Bye");
if(decision =='0'){Last edited by lk1001; 02-26-2010 at 04:42 PM.
Similar Threads
-
Problem with ordering for loops
By ScaryJello in forum New To JavaReplies: 3Last Post: 03-31-2009, 08:20 PM -
Date n Time Display problem
By kapilverma32 in forum Advanced JavaReplies: 6Last Post: 02-10-2009, 01:53 PM -
Struts 2 paging problem using display tag...?
By prabhurangan in forum Web FrameworksReplies: 0Last Post: 07-02-2008, 08:20 AM -
Problem with display the character
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 07:43 PM -
Problem with display in Netbeans 5.5
By Albert in forum NetBeansReplies: 1Last Post: 07-13-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks