我无法解决如何根据两个可能的选项获取输入并为这两个选项返回一个值。
例子:
您使用什么温度系统:摄氏度或华氏度
(做出选择后,在本例中为摄氏度,取答案并转换为华氏度)
输出两个温度
我试过“if else”和“switch”语句,但是当我选择摄氏度时,它会同时转换
System.out.println("Select which temperature scale you currently use, (c)elsius or (f)ahrenheit: ");
temperatureScale = scannerIn.next();
System.out.println("Temp in C is: " + temperatureInC);
*/
if (temperatureScale.equals("c")) {
System.out.println("Please enter the temperature in Celsius: ");
temperatureInC = scannerIn.nextDouble();
}
else {
System.out.println("Please enter the temperature in Fahrenheit: ");
temperatureInF = scannerIn.nextDouble();
}
tempInCelsiusConvFromFahrenheit = (temperatureInF - 32) * 5/9;
tempInFahrenheitConvFromCelsius = (temperatureInC * 9/5) + 32;
averageQuizScore = ((quiz1 + quiz2 + quiz3)/3);
System.out.println("*** Thank You ***");
System.out.println("Student EMPLID:" + studentID);
System.out.println("Quiz 1 Score: " + quiz1);
System.out.println("Quiz 2 Score: " + quiz2);
System.out.println("Quiz 3 Score: " + quiz3);
System.out.println("Average quiz score: " + df2.format(averageQuizScore));
System.out.println("Age in months: " + age * 12);
System.out.println("Age in years: " + age);
System.out.println("Temperature in Celsius: " + tempInCelsiusConvFromFahrenheit + '°');
System.out.println("Temperature is Fahrenheit: " + tempInFahrenheitConvFromCelsius + '°');
}
}
我选择摄氏度并输入 32。我以为我会得到 32 C 和 91.4 但我得到: 摄氏温度:-17.77777777777778° 温度是华氏温度:91.4°
一只斗牛犬
侃侃尔雅
相关分类