我正在编写一个简单的 Java 程序来进行温度转换,并且在第 8 行(我已在下面的代码中标记)上不断收到错误,指出 int 无法取消引用。我不确定这到底意味着什么,或者如何修复它,并且此处具有相同错误的其他问题无助于找到我的错误的解决方案。
import java.util.*;
class TemperatureConversion {
public TemperatureConversion()
{
int x = 0;
x.getInput.Int("Would you like to convert from Fahrenheit to Celcius or Celcius to Fahrenheit? Enter '1' for Fahrenheit to Celcius, and 2 for Celsius to Fahrenheit."); //This is the line with the error
if(x == 1)
{
toCelsius();
}
if(x ==2)
{
toFahrenheit();
}
}
public void toCelsius()
{
double fahrenheit = 0.0;
fahrenheit = Input.getDouble("Enter the temperature in Fahrenheit to be converted to Celsius.");
System.out.println("The converted value is " + (5.0/9.0 * (fahrenheit + 32)) + " degrees Celsius.");
}
public void toFahrenheit()
{
double celsius = 0.0;
celsius = Input.getDouble("Enter the temperature in Celsius to be converted to Fahrenheit.");
System.out.println("The converted value is " + (9.0/5.0 * (celsius + 32)) + " degrees Fahrenheit.");
}
}
最佳答案
x 是一个 int
,一个原语,因此不能取消引用 - 这意味着 x.anything 在 Java 中是无效语法。
我假设您想要做的是将一些用户输入分配给 x :
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
关于Java错误: int cannot be dereferenced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25110832/