Java 不兼容类型:可能从double转换为int
帮忙?我不知道为什么会出现这个错误。我在第39行:Java 不兼容类型:可能从double转换为int,java,Java,帮忙?我不知道为什么会出现这个错误。我在第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[
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;
}
}
公共类Calcprog{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
整数项=7;
双[]系数=新双[6];
双[]指数=新的双[6];
字符串[]项=新字符串[6];
System.out.println(“在多项式中输入项数:”);
numTerms=input.nextInt();
而(numTerms>6){
如果(numTerms>6){
System.out.println(“请将术语数量限制为六个。”);
System.out.println(“在多项式中输入项数:”);
numTerms=input.nextInt();
}
}
对于(int i=1;i
您试图将双参数传递给接受ints的方法,这需要强制转换,可能会导致信息丢失
您可以通过显式强制转换使其工作:
term[1] = differentiate((int)Coeff[1], (int)exponent[1]);
或者,您可以将differention
方法更改为接受双参数,这可能更有意义:
public String differentiate(double co, double exp)
将Differention方法的参数类型更改为double。这应该如下所示
public String differentiate(double co, double exp){
...
}
你的方法不是静态的,你调用的main是静态的,记住一个非静态方法可以在静态方法中直接访问,你必须创建一个类的实例来访问该方法,而且你传递的参数是
double
而不是int
。您的方法应该是这样的publicstaticstringdifferenting(双co,双exp){
您遇到了什么错误?不兼容的类型:可能存在从double到int的有损转换