gpt4 book ai didi

Java 汇总

转载 作者:搜寻专家 更新时间:2023-11-01 04:00:21 27 4
gpt4 key购买 nike

我怎样才能将 numberGrade 的值调高,如果它是 89.5,它会变成 90。numberGrade 被当作 double ,但将它设为 int 并不会向上或向下舍入。

public class GradeReporter
{
// The limit is the inclusive lower limit for each letter
// grade -- this means that 89.5 is an 'A' not a 'B'
public static final double A_LIMIT = 90;
public static final double B_LIMIT = 80;
public static final double C_LIMIT = 70;
public static final double D_LIMIT = 60;
public static final double F_LIMIT = 60;

/** Converts a numeric grade into a letter grade. Grades should be rounded to
* nearest whole number
*
* @param a numeric grade in the range of 0 to 100
* @returns a letter grade based on the numeric grade, possible grades are A, B, C, D and F.
*/
public char letterGrade(double numberGrade)
{
int grade = int(numberGrade);
if (grade >= A_LIMIT)
letterGrade = 'A';
else if (grade >= B_LIMIT)
letterGrade = 'B';
else if (grade >= C_LIMIT)
letterGrade = 'C';
else if (grade >= D_LIMIT)
letterGrade = 'D';
else if (grade < F_LIMIT)//4
letterGrade = 'F';
return letterGrade;
}

最佳答案

向上舍入,您可以使用Math.ceil(numberGrade)。要舍入到最接近的整数,请使用 Math.round(numberGrade)

参见:the Math class

关于Java 汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3705722/

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