基本上我试图让双打与整数一起工作,也许我遗漏了一些明显的东西,但是,使用返回的 highValue代码,我收到一个错误,上面写着“可能丢失精度,需要:int,找到:double ”。就评论而言,这是我正在上的一门课。这是代码:
package pj701;
public class PJ70103 {
public static void main(String[] args)
{ //DO NOT CHANGE ANYTHING IN THE MAIN()
double[] x = { 11.11, 66.66, 88.88, 33.33, 55.55 };
double[] y = { 9, 6, 5, 8, 3, 4, 7, 4, 6, 3, 8, 5, 7, 2 };
double[] z = { 123, 400, 765, 102, 345, 678, 234, 789 };
int index = FindIndexHighest (x);
System.out.printf( "%s%5d%s%8.2f\n", "Array x: element index = " , index,
" element contents = ", x[index] );
index = FindIndexHighest (y);
System.out.printf( "%s%5d%s%8.2f\n", "Array y: element index = " , index,
" element contents = ", y[index] );
System.out.printf( "%s%5d%s%8.2f\n", "Array z: element index = ",
FindIndexHighest (z), " element contents = ",
z[FindIndexHighest (z) ] );
}
//======================================================
// put your method definition here - - ONE method
//======================================================
public static int FindIndexHighest(double[] x)
{
double highValue = 0;
for (int i = 1; i < x.length; i++)
{
if (x[i] > highValue)
{
highValue = x[i];
}
else
{
highValue = highValue + 0;
}
}
return highValue;
}
}