问题
What can I do to fix this?
Here's part of the beginning of the code:
double fee;
double tuition;
double[] residence, total;
Here's the part where it's wrong:
total = tuition + fee;
error: incompatible types: double cannot be converted to double[]
What do I do to fix this?
回答1:
What you've done here is declared 4 variables: fee and tuition are of type double, while residence and total are of type double[] -- i.e. an array of elements of type double.
You're adding up tuition and fee, and what the compiler is expecting you to do is to put the result into another variable of type double, but your code is asking to store it into total which is of type double[] (array of double), and the compiler doesn't know how to resolve that.
You can either
Tell the compiler which element of
totalto store the result in, for example:total[0] = tuition + feeDeclare
totalas having the type of a singledoubleinstead of an array:double fee; double tuition; double total; double[] residence; // this is now okay total = tuition + fee; // this is again a type error because residence is still an array residence = total;
回答2:
For an array you must specify an index. Like:
total[0] = tuition + fee;
An array is a collection of something in this case doubles an array cannot equal one double it can have several double values at different indexes of the array.
回答3:
I think you meant to do this, correct me if I'm wrong
double tuition, residence, total;
The way you did it results in residence and total being arrays of double values, and not a double value like tuition.
来源:https://stackoverflow.com/questions/27381737/after-i-compile-my-java-program-it-says-error-incompatible-types-double-canno