Java 转换错误:意外类型。必需:变量,找到:值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18820089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:21:11  来源:igfitidea点击:

Java casting error: unexpected type. required: variable, found: value

javacasting

提问by Ivan_Stepul

In Java, I am trying to cast an int into a double and then back an int.

在 Java 中,我试图将 int 转换为 double,然后返回一个 int。

I am getting this error:

我收到此错误:

unexpected type
          (double)(result) =  Math.pow((double)(operand1),(double)(operand2));
          ^
  required: variable
  found:    value

From this code:

从这个代码:

(double)(result) =  Math.pow((double)(operand1),(double)(operand2));
return (int)(result);

What does the error message mean?

错误信息是什么意思?

回答by UFL1138

You don't need to cast int to double in order to call Math.pow:

您无需将 int 转换为 double 即可调用 Math.pow:

package test;

public class CastingTest {
    public static int exponent(int base, int power){
        return ((Double)Math.pow(base,power)).intValue();
    }
    public static void main(String[] arg){
        System.out.println(exponent(5,3));
    }
}

回答by MadProgrammer

Let's assume that resultis actually a double, then you would simply only need to do...

让我们假设它result实际上是 a double,那么您只需要做...

result = Math.pow((double)(operand1),(double)(operand2));

Now, let's assume that resultis actually int, then you simple need to do...

现在,让我们假设result实际上是这样int,那么您只需要做...

result = (int)Math.pow((double)(operand1),(double)(operand2));

Updated

更新

With feedback from Patricia Shanahan, there is a lot of unnecessary noise in the code. Without further context, it is difficult to comment entirely, but, it is unlikely (and unhelpful) to case operand1and operand2explicitly to double. Java is capable of taking care of this by itself.

根据 Patricia Shanahan 的反馈,代码中有很多不必要的噪音。没有进一步的上下文,很难完全评论,但是,不太可能(并且无益)区分operand1operand2明确表示double. Java 能够自己处理这个问题。

Math.pow(operand1, operand2);

回答by Dawood ibn Kareem

The message just means that you've messed up the syntax. The casting needs to go on the right hand side of the equals, not in front of the variable that you're assigning to.

该消息仅表示您弄乱了语法。转换需要在等号的右侧进行,而不是在您分配给的变量前面。

回答by Eric Leschinski

This code in Java:

此代码在 Java 中:

double my_double = 5;
(double)(result) = my_double;  

Will throw an compile time error:

会抛出编译时错误:

The left-hand side of an assignment must be a variable

Casts are not allowed on a variable to the left of the equals sign being assigned to. It doesn't even make sense what the code would mean. Are you trying to remind the compiler that your variable is a double? As if it didn't already know?

被赋值的等号左边的变量不允许强制转换。代码的含义甚至没有意义。你是想提醒编译器你的变量是双精度的吗?好像它不知道?

回答by necromancer

you probably intend:

你可能打算:

double result =  Math.pow((double)(operand1),(double)(operand2));
return (int)(result);

or equivalently but more simply:

或等效但更简单:

double result =  Math.pow((double)operand1,(double)operand2);
return (int)result;

or even:

甚至:

return (int)Math.pow((double)operand1,(double)operand2);