嗨,所以我想将一个优先级队列以Heap降序实现,从大到小,我希望使用指数。所以我整理了我的堆和优先级队列类,但是现在在做我的类时,我收到一条错误消息:
xponents.java:66: error: invalid method declaration; return type required
public Exponent(int num, int exponent, int x)
我不确定我哪里出错了
public class Exponents implements Comparable<Exponents>
{
private int num;
private int exponent;
private int x;
public Exponents(int num, int exponent, int x)
{
this.num = num;
this.exponent = exponent;
this.x = x;
}
public int Answer()
{
int x = Math.pow(num,exponent);
return x;
}
public String toString()
{
String s = ("Exponent: " + exponent + "\n Number: "+ num + "\nYour x is: )" + x);
return s;
}
public int compareTo(Answer e)
{
return this.x - o.x;
}
}
由于出现错字,问题已得到解决,现在我收到了更多错误,我不确定是我写错了什么:
Exponents.java:83: error: cannot find symbol
public int compareTo(Answer e)
^
symbol: class Answer
location: class Exponents
Exponents.java:74: error: incompatible types: possible lossy conversion from double to int
int x = Math.pow(num,exponent);
^
Exponents.java:85: error: cannot find symbol
return this.x - o.x;
^
symbol: variable o
location: class Exponents
3 errors
最佳答案
您的构造函数中有一个错字。您的类名为Exponents
,但是在您的构造函数中,它说Exponent
。由于您正在寻找返回数据类型,因此它成为无效的方法声明,因为您创建的“构造函数”的名称与类的名称不同。
编辑:
Answer
。 Math.pow(num,exponent).intValue()
o
的变量。 关于java - 优先队列实现堆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43304890/