java - Incompatible types: possible lossy conversion from double to int -
help? don't know why getting error. getting @ in line 39:
term[1] = differentiate(coeff[1], exponent[1]);
how can fix issue?
full code listing:
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 number of terms in polynomial:"); numterms = input.nextint(); while (numterms > 6) { if (numterms > 6) { system.out.println("please limit number of terms six."); system.out.println("enter number of terms in polynomial:"); numterms = input.nextint(); } } (int = 1; < numterms + 1; i++) { system.out.println("please enter coefficient of term #" + + " in decimal form:"); coeff[i] = input.nextdouble(); system.out.println("please enter exponent of term #" + + " 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; } }
you trying pass double arguments method accepts ints, requires casting may result in loss of information.
you can make work explicit cast :
term[1] = differentiate((int)coeff[1], (int)exponent[1]);
or can change differentiate
method accept double arguments, make more sense :
public string differentiate(double co, double exp)
Comments
Post a Comment