M
machoextreme
Once again, I'm having trouble with some code (this group has already
gotten me out of a few jams). I'm writing a seemingly simple
temperature conversion program, and it works great. I have gotten it to
compile and everything. The problem; I can't get it to loop (yes this
is my first attempt to work a loop into a program). I was trying to
allow the user to return to the initiation code at the beggining of the
program and repeat the options using to Do-While method, but I must be
getting the placement of the while statement, or something equally
stupid, but I just can't figure this help. Could someone look at my
code and instruct me on how exactly to work a loop into a program?!?
import java.util.*;
public class mlounsburyproject2
{
public static void main(String[] args)
{
do{
float degreesC;
float degreesF;
String nextent= "";
Scanner keyboard= new Scanner(System.in);
System.out.println("Welcome to the temperature convertor program");
System.out.println("Please choose the temperature format you wish to
convert from, C or c for celsius, F or f for Fahrenheit");
String format = keyboard.next(); // returns a single word excluding
blanks
char letter = format.charAt(0); // get the first character which is
supposed to be the one the program is waiting for
if (format.equalsIgnoreCase ("c"))
{
System.out.println("Please enter the temperature in Celcius that is
to be converted" );
degreesC= keyboard.nextFloat();
degreesF= ((9 * (degreesC)/5) + 32);
System.out.println("The converted temperature is " +degreesF +"
Fahrenheit");
System.out.print("Press q or Q to quit the program, or any other key
to start over ");
nextent= keyboard.next();
if (nextent.equalsIgnoreCase("q"))
System.out.println("Goodbye");
System.exit(0);
}
else if (format.equalsIgnoreCase("f"))
{
System.out.println("Please enter the temperature in Fahrenheit that
is to be converted" );
degreesF= keyboard.nextFloat();
degreesC= (5 *(degreesF - 32)/9);
System.out.println("The converted temperature is " +degreesC + "
Celsius");
System.out.print("Press q or Q to quit the program, or any other key
to start over ");
nextent= keyboard.next();
if (nextent.equalsIgnoreCase("q"))
System.out.println("Goodbye");
System.exit(0);
}
else
{
System.out.println("That is not a valid selection, please try again
");
}
}while (true);
}
}
gotten me out of a few jams). I'm writing a seemingly simple
temperature conversion program, and it works great. I have gotten it to
compile and everything. The problem; I can't get it to loop (yes this
is my first attempt to work a loop into a program). I was trying to
allow the user to return to the initiation code at the beggining of the
program and repeat the options using to Do-While method, but I must be
getting the placement of the while statement, or something equally
stupid, but I just can't figure this help. Could someone look at my
code and instruct me on how exactly to work a loop into a program?!?
import java.util.*;
public class mlounsburyproject2
{
public static void main(String[] args)
{
do{
float degreesC;
float degreesF;
String nextent= "";
Scanner keyboard= new Scanner(System.in);
System.out.println("Welcome to the temperature convertor program");
System.out.println("Please choose the temperature format you wish to
convert from, C or c for celsius, F or f for Fahrenheit");
String format = keyboard.next(); // returns a single word excluding
blanks
char letter = format.charAt(0); // get the first character which is
supposed to be the one the program is waiting for
if (format.equalsIgnoreCase ("c"))
{
System.out.println("Please enter the temperature in Celcius that is
to be converted" );
degreesC= keyboard.nextFloat();
degreesF= ((9 * (degreesC)/5) + 32);
System.out.println("The converted temperature is " +degreesF +"
Fahrenheit");
System.out.print("Press q or Q to quit the program, or any other key
to start over ");
nextent= keyboard.next();
if (nextent.equalsIgnoreCase("q"))
System.out.println("Goodbye");
System.exit(0);
}
else if (format.equalsIgnoreCase("f"))
{
System.out.println("Please enter the temperature in Fahrenheit that
is to be converted" );
degreesF= keyboard.nextFloat();
degreesC= (5 *(degreesF - 32)/9);
System.out.println("The converted temperature is " +degreesC + "
Celsius");
System.out.print("Press q or Q to quit the program, or any other key
to start over ");
nextent= keyboard.next();
if (nextent.equalsIgnoreCase("q"))
System.out.println("Goodbye");
System.exit(0);
}
else
{
System.out.println("That is not a valid selection, please try again
");
}
}while (true);
}
}