我的代码给我44400.0%
。我已经搞砸了一堆,但这是最接近我的正确答案。我只允许改变我已经定义了temp数组和下面的行的内容。当我尝试划分更多时,我输掉了有效数字,因为我需要四分之三作为百分比的一部分。从整数不正确的百分比
import java.text.DecimalFormat;
public class ArrayElements90OrMore
{
public static void main(String [] args)
{
int [] array = { 91, 82, 73, 94, 35, 96, 90, 89, 65 };
DecimalFormat percent = new DecimalFormat("#0.0%");
System.out.print("The elements are ");
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
System.out.println();
System.out.println("The percentage of elements 90 or greater is "
+ percent.format(percent90OrMore(array)));
}
public static int percent90OrMore(int [] temp)
{
int count = 0;
for (int i = 0; i < temp.length; i++) {
if (temp[i] >= 90) {
count++;
}
}
return (count * 1000)/(temp.length);
}
}
这是真的让我烦恼的事情。我不应该改变第一部分,它不会接受双倍。如果它接受双倍的话,这很容易。 – dsorenson 2014-12-07 09:12:25
@dsorenson'percent.format()'确实接受double。哪一部分不能改变?您只需更改'percent90OrMore' – Eran 2014-12-07 09:15:14
我无法通过上一个打印语句更改任何内容。当我使用这行代码:return((double)count * 100)/ temp.length; 我收到此错误: ArrayElements90OrMore.java:35:错误:可能丢失精度 返回((double)count * 100)/ temp.length; ^ 必需:int 找到:双重 1错误 – dsorenson 2014-12-07 09:18:17