帮助?我不知道为什么会收到此错误。我在第39行:
term[1] = differentiate(Coeff[1], exponent[1]);
如何解决此问题?
完整代码列表:
public class Calcprog {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numTerms = 7;
double[] Coeff = new double[6];
double[] exponent = new double[6];
String[] term = new String[6];
System.out.println("Enter the number of terms in your polynomial:");
numTerms = input.nextInt();
while (numTerms > 6) {
if (numTerms > 6) {
System.out.println("Please limit the number of terms to six.");
System.out.println("Enter the number of terms in your polynomial:");
numTerms = input.nextInt();
}
}
for (int i = 1; i < numTerms + 1; i++) {
System.out.println("Please enter the coefficient of term #" + i + " in decimal form:");
Coeff[i] = input.nextDouble();
System.out.println("Please enter the exponent of term #" + i + " in decimal form:");
exponent[i] = input.nextDouble();
}
term[1] = differentiate(Coeff[1], exponent[1]);
}
public String differentiate(int co, int exp) {
double newco, newexp;
String derivative;
newexp = exp - 1;
newco = co * exp;
derivative = Double.toString(newco) + "x" + Double.toString(newexp);
return derivative;
}
}
答案 0 :(得分:3)
您正在尝试将双参数传递给接受整数的方法,这需要一个可能导致信息丢失的强制转换。
你可以通过一个明确的演员来使它工作:
term[1] = differentiate((int)Coeff[1], (int)exponent[1]);
或者您可以更改differentiate
方法以接受双重参数,这可能更有意义:
public String differentiate(double co, double exp)
答案 1 :(得分:1)
你的方法不是静态的,你在main中调用是静态的,记住一个非静态方法可以直接在静态方法中访问,你必须创建一个类的实例来访问该方法,还有您传递的参数是double
而不是int
。您的方法应该与public static String differentiate(double co, double exp){
答案 2 :(得分:0)
将Differeate方法的参数类型更改为double。这应该如下所示
public String differentiate(double co, double exp){
...
}