我正在尝试使用不同的方法进行温度转换器。现在,我现在有点卡住了。我正在一个方法中创建一个开关,它有一个char,一个double和另一个char作为参数,这3个表示临时转换,实际温度本身和用户想要转换的温度。我一直试图创建这个开关,但到目前为止我没有运气。也许我不是在说清楚自己。抱歉!!我认为很明显我是初学者,这是家庭作业。我不是要求获得整个代码,只需要几个提示再次回到正轨。非常感谢! : - )
这是我想要切换的方法,但是我需要来自另一个的信息 我将在这个下面发布的方法
public static double convertTemp( char uFrom, double temp, char uTo ){
}
此方法将执行从摄氏度到开尔文或华氏度的实际转换。
public static double convFromCelsius( double value, char unitTo ){
}
再次感谢!
这是我到目前为止所得到的。
import java.util.Scanner;
class mainAssignment {
// 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(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(tempFrom);
/*
Method to get the scale to which the
temperature value will be converted to
*/
scaleTo = getUnitTo(scaleTo);
// 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 ){
}
// Method to convert temperatures in Celsius to the other ones
public static double convFromCelsius( double value, char unitTo ){
}
// Method to convert temperatures in Fahrenheit to the other ones
//public static double convFromFahrenheit( double value, char unitTo ){
// body of the Method
//}return;
// Method to convert temperatures in Kelvin to the other ones
//public static double convFromKelvin( double value, char unitTo ){
// body of the Method
//}return;
public static char displayMenu (char scaleFrom){
Scanner ui = new Scanner (System.in);
System.out.println ("");
System.out.println ("============================");
System.out.println (" Temperature Conversion");
System.out.println ("=========== MENU ===========");
System.out.println ("");
System.out.println ("a. From Celsius");
System.out.println ("b. From Fahrenheit");
System.out.println ("c. From Kelvin");
System.out.println ("");
System.out.println ("x. Exit");
System.out.println ("");
System.out.println ("============================");
System.out.println ("Enter an option: ");
System.out.println ("");
scaleFrom = ui.nextLine().charAt(0);
return scaleFrom;
}
public static double getTemp (double getTemp){
Scanner ui = new Scanner (System.in);
System.out.println ("");
System.out.println ("Please, enter the temperature you want to convert: ");
System.out.println ("");
getTemp = Double.parseDouble(ui.nextLine());
return getTemp;
}
public static char getUnitTo (char scaleTo){
Scanner ui = new Scanner (System.in);
System.out.println ("");
System.out.println ("Please, choose the temperature you want to convert to:");
System.out.println ("");
System.out.println ("C = To Celsius K = To Kelvin F = To Fahrenheit");
System.out.println ("");
scaleTo = ui.nextLine().charAt(0);
return scaleTo;
}
} //主要作业的结束//
答案 0 :(得分:0)
我想你想要这样的东西:
public static double convertTemp( char uFrom, double temp, char uTo ){
switch (uFrom) {
case 'c':
return convertFromCelsius(temp, uTo);
case 'f':
return convertFromFahrenheit(temp, uTo);
case 'k':
return convertFromKelvin(temp, uTo);
default:
System.out.println("Unexpected unit");
return 0;
}
}
public static double convFromCelsius( double value, char unitTo ){
switch (unitTo) {
case 'f':
// Code to convert from c to f
return value + 30;
case 'k':
// Code to convert from c to k
return value + 273;
default:
System.out.println("Unexpcted unit");
return 0;
}
}