Java 开关和方法
我试着用不同的方法做一个温度转换器。现在,我有点被卡住了。我在一个方法中创建了一个开关,它有一个char,一个double和另一个char作为参数,这3个参数表示转换的温度,实际温度本身和用户想要转换的温度。我一直在尝试创建此开关,但到目前为止,我没有任何运气。也许我说得不够清楚。很抱歉我想很明显,我是一个初学者,这就是家庭作业。我不是要得到全部的代码,只是一些提示,让我们重新回到正轨。非常感谢!:-) 这是我想要切换到的方法,但是我需要从另一个输入的信息 我将在下面发布的方法Java 开关和方法,java,methods,switch-statement,converter,temperature,Java,Methods,Switch Statement,Converter,Temperature,我试着用不同的方法做一个温度转换器。现在,我有点被卡住了。我在一个方法中创建了一个开关,它有一个char,一个double和另一个char作为参数,这3个参数表示转换的温度,实际温度本身和用户想要转换的温度。我一直在尝试创建此开关,但到目前为止,我没有任何运气。也许我说得不够清楚。很抱歉我想很明显,我是一个初学者,这就是家庭作业。我不是要得到全部的代码,只是一些提示,让我们重新回到正轨。非常感谢!:-) 这是我想要切换到的方法,但是我需要从另一个输入的信息 我将在下面发布的方法 public s
public static double convertTemp( char uFrom, double temp, char uTo ){
}
此方法将执行从摄氏到开尔文或华氏的实际转换
public static double convFromCelsius( double value, char unitTo ){
}
再次非常感谢
这是我到目前为止得到的
导入java.util.Scanner
课堂作业{
// 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;
}
}//课后主要作业//我想你想要的是:
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;
}
}
为什么您需要一个
temp
变量?在摄氏度、华氏度和开尔文之间的转换是定义良好的公式。请提供这些方法的输入和输出示例。抱歉,我不明白。你能再具体一点吗?干杯。开关在哪里?你试过了吗?不,就是这样。我还不知道怎么换。干杯