Java错误: int cannot be dereferenced

标签 java

我正在编写一个简单的 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/

相关文章:

java - Woocommerce API 获取所有产品

java - 如何在三个不同的 Java IDE 上检查已安装的 JAR、外部库等?

java - 为什么这不起作用?

java - 使用 Perlin 噪声生成 2d 瓦片 map

java - 将比较器传递给 java Collections.disjoint

java - 使用 Java Graphics2D API 在 TextLayout 中将文本右对齐

java - 如何告诉 EclipseLink 在 SQL 中使用完整的表名称作为别名

java - 泛型方法的不同返回类型取决于调用位置

java - 你能解释一下 Java 中装箱和泛型的奇怪行为吗

Java 使用 Mockito 验证 void 方法调用 n 次