我正在尝试创建一个简单的程序,通过终端应用程序并使用 vi 编辑器,在我的 Mac 上将华氏度转换为摄氏度。
我的代码如下:
import java.io.*;
public class MyThirdJavaProgram
{
public static void main (String [] args)
{
double celcius, fahrenheit;
fahrenheit = Console.readDouble("fahrenheit");
celcius = convertFahrenheitToCelcius(fahrenheit);
System.out.println("Degrees in celcius are " + celcius);
}
private static double convertFahrenheitToCelcius (double ins)
{
double celcius;
celcius = ((ins-32.0)*5.0)/9.0;
return celcius;
}
}
我不断收到的错误是:
MyThirdJavaProgram.java:7: cannot find symbol
symbol : method readDouble(java.lang.String)
location: class java.io.Console
fahrenheit = Console.readDouble("fahrenheit");
^
我在这里做错了什么?我尝试了很多不同的方法来编码,例如
fahrenheit = Console.in.readDouble(fahrenheit)
和
fahrenheit = System.console.readDouble(fahrenheit)
但绝对没有任何效果,并且错误语句每次都几乎相同。我对java很陌生,但这让我很困惑。任何解决这个该死的事情的帮助都会很棒,即使你告诉我重写一半。
最佳答案
Console API 中没有方法 readDouble(String)
。请使用java.util.Scanner
。示例Scanner.nextDouble()
关于java - 如何在 Mac 上的终端中获取 java 程序的控制台输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22625623/