本文介绍了不兼容的类型:可能从双精度整数转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有帮助吗?我不知道为什么会收到此错误.我进入第39行:
Help? I don't know why I am getting this error. I am getting at in line 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;
}
}
推荐答案
您正试图将双参数传递给接受int的方法,该方法需要进行强制转换,这可能会导致信息丢失.
You are trying to pass double arguments to a method that accepts ints, which requires a casting that may result in loss of information.
您可以通过显式强制转换使其工作:
You can make it work by an explicit cast :
term[1] = differentiate((int)Coeff[1], (int)exponent[1]);
或者您可以将您的 differentiate
方法更改为接受双参数,这可能更有意义:
Or you can change your differentiate
method to accept double arguments, which would probably make more sense :
public String differentiate(double co, double exp)
这篇关于不兼容的类型:可能从双精度整数转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!