gpt4 book ai didi

java - 不兼容的类型 - double 到 int?

转载 作者:行者123 更新时间:2023-12-01 16:59:33 31 4
gpt4 key购买 nike

Java 似乎认为我正在尝试对我的双变量之一进行转换或执行某种操作。我收到错误消息

average2.java:23: error: incompatible types: possible lossy conversion from double to int scores[count++]=score;

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

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

最佳答案

错误涉及 count 变量,它是一个 double。但是 int 是数组的有效索引。该错误是由于使用 double 作为索引而导致的,而索引应为 int

count 声明为 int

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

关于java - 不兼容的类型 - double 到 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28751572/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com