| Linette411 |
7Mar2012 12:23 |
Please Help -- Do While Loop continues disregarding boolean statement
Hi, I am a beginner in programming and for this assignment (for an online class) I cannot seem to figure out how to fix/what I am doing wrong and since its an online class I really cant ask for help.Please help! So in my code I would like the user to enter a scale either 'c' or 'f'....now if you enter anything else it catches the error but then still proceeds! also, when rounding the calculated conversion...i am not getting a rounded answer only **.00 the asterisks being any numeral. here is my code:
Code:
package temperatureconversion;
import java.util.Scanner;
import java.text.DecimalFormat;
/**
*
* @author Linette
*/
public class TemperatureConversion {
public static void main(String[] args) {
System.out.println("\t\t\tHello " + System.getProperty("user.name") + "!");
String os = System.getProperty("os.name");
String version = System.getProperty("os.version");
System.out.println("\tI currently am running "+os+", version "+version);
boolean correctScale = true;
Scanner keyboard = new Scanner(System.in);
int tempPicked;
System.out.println("Enter a temperature scale: 'F' for fahrenheit or 'C' for celsius)");
String scaleType = keyboard.nextLine();
scaleType = scaleType.toUpperCase();
char answerScale = scaleType.charAt(0);
do
{
if (answerScale != 'F' && answerScale != 'f' && answerScale != 'C' && answerScale != 'c')
System.out.println("Error: Scale not found please enter 'F' for Fahrenheit or 'C' for Celsius");
else
System.out.println("You have choosen the '" + answerScale + "' scale.");
System.out.println("Please enter a temperature (whole integer,no fractions)");
tempPicked = keyboard.nextInt();
Scanner scan = new Scanner(System.in);
double convertedCelsius, convertedFahrenheit;
DecimalFormat formatter=new DecimalFormat("0.00");
switch (answerScale)
{
case 'f':
convertedCelsius = (5*(tempPicked-32))/9;
System.out.println("Converts to = ");
Double.toString(convertedCelsius);
System.out.println(formatter.format(convertedCelsius)+ " degrees Celsius");
break;
case 'F':
convertedCelsius = (5*(tempPicked-32))/9;
System.out.println("Converts to = ");
Double.toString(convertedCelsius);
System.out.println(formatter.format(convertedCelsius)+ " degrees Celsius");
break;
case 'C':
convertedFahrenheit = (9*tempPicked)/5 +32;
System.out.println("Converts to = ");
Double.toString(convertedFahrenheit);
System.out.println(formatter.format(convertedFahrenheit)+ " degrees Fahrenheit");
break;
case 'c':
convertedFahrenheit = (9*tempPicked)/5 +32;
System.out.println("Converts to = ");
Double.toString(convertedFahrenheit);
System.out.println(formatter.format(convertedFahrenheit)+ " degrees Fahrenheit");
break;
default:
System.out.println("Please enter a valid character to set scale:");
break;
}
} while (correctScale = false);
}
}
|