我正在尝试做温度转换,并且我知道我需要在main中调用convertTemp,但我只是不知道自己在做什么。有人可以看看这个并帮助我吗?如何调用另一个方法
import java.util.Scanner;
public class TemperatureConverter {
public static void convertTemp() {
Scanner keyboard = new Scanner(System.in);
double temperature;
String temperatureScale = " ";
if (temperatureScale.equals("f"))
{
// code that converts from Fahrenheit to Celsius
temperature = (5/9)*(keyboard.nextDouble() - 32);
// and prints the result to the screen
System.out.println("The temperature is " + temperature + "degrees celsius");
}
//
else if (temperatureScale.equals("c"))
{
// code that converts from Celsius to Fahrenheit
temperature = 32.0 +(keyboard.nextDouble() * 1.8);
System.out.println("The temperature is " + temperature + "degrees fahrenheit");
// and prints the result to the screen
}
else
{
// code that outputs a message indicating that an incorrect
System.out.println("Error! A valid temperature was not chosen!");
// option was selected
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("What temeprature number are you trying to find out?");
double keyboardInput = keyboard.nextDouble();
System.out.println("Type f for Fahrenheit or c for Celsius.");
String keyboardTempLetter = keyboard.next();
}
}
//}
对于初学者来说:'temperatureScale'应该是一个参数'convertTemp',从'main'传入。 –
好吧,“应该”是指它会改善代码,而不是它需要执行。 – markspace
我同意@LouisWasserman:I/O不应该成为此方法的一部分。它应该做一件事:将温度从一种尺度转换为另一种尺度。 – duffymo