Olá pessoal !!
Sou novato em java e estou tentanto fazer um conversor de temperatura, os comentarios estào em inglês porque estudo no exterior e meu professor e casca grossa com relaçao a comentários, mais até então acho que só pelo código vocês entenderão com certeza .
E seguinte e um conversor de temperatura de diferentes escalas:
1 -Mostra o menu e a opç~ao na tela para o usuario escolher.
2 - pega a temperatura para ser convertida.
3 - pega a unidade da temperatura de cada temperatura a ser convertida(Celsius, Fahrenheit e Kelvin).
4 - Converte a temperatura;
5 - Mostra o resultado na tela.
Quando executo o resultado so me mostra 0.0 e nunca o valor, acho que algum erro no metodo não sei, segue o que fiz até agora.
Muito obrigado desde já.
import java.util.Scanner;
public class tempConversion {
// Main Method
public static void main( String[] args ){
char scaleFrom = ' '; // From which temperature scale to convert from
char scaleTo = ' '; // To which temperature scale to convert to
double tempFrom = 0.0; // Temperature value to be converted
double tempTo = 0.0; // Temperature value converted
double result = 0.0; // Result of the conversion
// Loop to repeat the menu until option chosen is "x"
do {
/*
Method to display the menu and store the scale from
which the temperature will be converted from
*/
scaleFrom = displayMenu(tempFrom, scaleTo, scaleFrom);
/*
Only asks user to input more information,
if scaleFrom is different than "x" ( x = Exit )
*/
if ( scaleFrom != 'x' ){
/*
Method to get the temperature value to be
converted and store the value entered by user
*/
tempFrom = getTemp();
/*
Method to get the scale to which the
temperature value will be converted to
*/
scaleTo = getUnitTo();
// tempTo = getTemp();
// Method to convert the Temperature
result = convertTemp( scaleFrom, tempFrom, scaleTo );
// Method to display the conversion to the screen
displayResult( scaleFrom, tempFrom, scaleTo, result );
}
} while ( scaleFrom != 'x' );
}
// Method to invoke the conversion of the temperature
public static double convertTemp( char uFrom, double temp, char uTo ){
// body of the Method
if(uFrom=='a'){
tempConversion.convFromCelsius(temp, uTo);
}else if(uFrom=='b'){
tempConversion.convFromFahrenheit(temp, uTo);
}else if(uFrom=='c'){
tempConversion.convFromKelvin(temp, uTo);
}
return uTo;
}
// Method to convert temperatures in Celsius to the other ones
public static double convFromCelsius( double value, char unitTo ){
// body of the Method
//value = tempConversion.convFromCelsius(getTemp(), unitTo);
//unitTo= tempConversion.convFromCelsius(getTemp(), unitTo);
if(unitTo=='b'){//celsius to Fahrenheit
double newvalue= (value * 9/5) +32;
return newvalue;
} else if(unitTo=='c') {//celsius to kelvin
double newvalue=(5/9 * (value - 32) + 273.15 );
return newvalue;
}
return value;
}
// Method to convert temperatures in Fahrenheit to the other ones
public static double convFromFahrenheit( double value, char unitTo ){
// body of the Method
if(unitTo=='a'){//fahrenheit to celsius
double newvalue=((value - 32) * 5/9);
return newvalue;
} else if(unitTo=='c') {//fahrenheit to kelvin
double newvalue=(value + 459.67) * 5/9;
return newvalue;
}
return value;
}
// Method to convert temperatures in Kelvin to the other ones
public static double convFromKelvin( double value, char unitTo ){
// body of the Method
if(unitTo=='a'){//kelvin to Celsius
value = value-273.15;
return value;
} else if(unitTo=='b') {//kelvin to fahreinheit
value = value * 9/5 - 459.67;
return value;
}
return value;
}
private static void displayResult(char scaleFrom, double tempFrom,char scaleTo, double result) {
System.out.println("first option: "+scaleFrom+
"\nTemperature:"+tempFrom+
"\nTo be convert to:"+scaleTo+
"\nThe result is:"+result);
}
private static double getTemp() {
System.out.println("Enter the temperature: ");
Scanner sc = new Scanner(System.in);
Double.parseDouble(sc.nextLine());
return 0;
}
private static char getUnitTo() {
System.out.println();
System.out.println("============================"+
"\nTemperature Conversion"+
"\n=========== MENU ==========="+
"\na. To Celsius"+
"\nb. To Fahrenheit"+
"\nc. To Kelvin"+
"\nx. Exit"+
"\n============================"+
"\nEnter an option:");
try {
Scanner sc = new Scanner(System.in);
String menu;
menu = sc.nextLine();
switch (menu) {
case "a":
//System.out.println("Celsius");
break;
case "b":
//System.out.println("Fahrenheit");
break;
case "c":
//System.out.println("Kelvin");
break;
case "x":
System.out.println("Good bye!!");
System.exit(0);
break;
default:
System.out.println("Please enter a valid value");
break;
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
return 0;
}
private static char displayMenu(double value, char unitTo, char menu) {
System.out.println();
System.out.println("============================"+
"\nTemperature Conversion"+
"\n=========== MENU ==========="+
"\na. From Celsius"+
"\nb. From Fahrenheit"+
"\nc. From Kelvin"+
"\nx. Exit"+
"\n============================"+
"\nEnter an option:");
try {
Scanner sc = new Scanner(System.in);
String menu1;
menu1 = sc.nextLine();
switch (menu1) {
case "a":
//System.out.println("Celsius");
tempConversion.convFromCelsius(value, unitTo);;
break;
case "b":
tempConversion.convFromFahrenheit(value, unitTo);;
//System.out.println("Fahrenheit");
break;
case "c":
tempConversion.convFromKelvin(value, unitTo);
//System.out.println("Kelvin");
break;
case "x":
System.out.println("Good bye!!");
System.exit(0);
//exit = true;
break;
default:
System.out.println("Please enter a valid value");
break;
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return menu ;
}
}