gpt4 book ai didi

java - 我的表输出的百分比值不正确

转载 作者:行者123 更新时间:2023-12-01 17:24:39 25 4
gpt4 key购买 nike

public class apples {

private static String[] level1 = new String[] { "A", "B", "I", "K", "N", "O", "P", "S", "T", "W" };



public static void main(String[] args) {

int[] scores1 = { 99, 80, 56, 88, 70, 35, 67, 60, 78, 56 };

int[] correct1 = {20, 20, 13, 15, 22, 18, 19, 21, 23, 25};

int[] incorrect1 = {2, 1, 5, 2, 2, 5, 8, 1, 0, 0};


double[] percentage1 = new double[correct1.length];
for(int a = 0; a < correct1.length; a++ ){
percentage1[a] = (double)((correct1[a] / (correct1[a] + incorrect1[a]))*100);
}


System.out.println("Character \t Correct \t Incorrect \t Percentage");
for(int counter = 0; counter<scores1.length;counter++){
System.out.println(level1[counter] + "\t\t " + correct1[counter] + "\t\t " + incorrect1[counter] + "\t\t " + percentage1[counter]);
}


}

}

这将输出一个包含 4 个标题的表格。字符、正确和错误列按预期显示。但是百分比行无法正常工作。例如,字符“A”、正确 20 和错误 2 给出的百分比为 0.0。任何“不正确”值 > 0 都会输出百分比值 0,任何 = 0 的“不正确”值都会输出百分比值 100(这是正确的)...有人可以解释一下我哪里出了问题吗?

最佳答案

这里处理的是整数,对于整数除法,结果会被截断。您需要将原始值转换为 double,或者将一部分乘以 1.0 以获得 double 值:

percentage1[a] = ((correct1[a]*1.0 / (correct1[a] + incorrect1[a]))*100);

关于java - 我的表输出的百分比值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15999491/

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