不兼容的类型 - double到int?

时间:2015-02-26 19:55:57

标签: java

Java似乎认为我试图在我的一个双变量上转换或执行某种操作。我收到错误消息

  

average2.java:23:错误:不兼容的类型:可能有损   从double转换为int得分[count ++] =得分;

我真的很困惑,因为到目前为止我还没有声明任何整数, - 每个变量都是双精度因为我希望有一些小数。以下是我的代码:

public static void main (String [] args)
{

    double numOf;
    double lowest = 100;
    double count = 0;
    double sum = 0;

    double average = 0;
    double score;

    double scores[] = new double[100]; //[IO.readDouble("Enter Scores, Enter -1 to Quit")];

    while ((count <100) &&(( score =IO.readDouble("Enter Scores,  (-1 to Quit")) > 0));
    {
        scores[count++]=score;
    }

    //This section obtains the highest number that was entered`
    double max = scores[0];
    for (double i=0; i<scores.length; i++)
        if(max < scores[i])max =scores[i];

    System.out.println("Maximum is " + max);    

    // This section obtains the lowest score entered    
    double min = scores[0];
    for (int i=0; i<scores.length; i++)
        if (min > scores[i]) min = scores [i];

    int sumOf =0;
    for (int i=0; i < scores.length; i++)
    {    
        sumOf += scores[i];     
    }
    System.out.println("The sum of all scores is  " + sumOf);

    System.out.println("Minimum is " + min);

    count = count + 1;                     

    average =  (sumOf/scores.length);     
    System.out.println("Average is " + average);
} //end main      
} //end class     

1 个答案:

答案 0 :(得分:5)

错误是指count变量,即double。但是int是数组的有效索引。使用double作为索引时会出现int,从而导致错误。

count声明为int

出于同样的原因,您还应该在第一个i循环中将int声明为for