-1

嘿,我无法弄清楚如何将类型返回为字符串,因为我已将 outputGrade 定义为字符串,但我正在尝试计算单个数字。例如-mark=79 然后 outputGrade 字符串 = "9"

private static String printResult (int numAssignments, double  studentMark)
        {
        int outputGradeSingle;
        String outputGrade;
        switch (numAssignments)
        {
        case 0:  outputGrade="DNA-";
        break;
        case 1: case 2: case 3: case 4: outputGrade="DNC-";
        break;
        case 5: if (studentMark<50.0)
                {
                outputGrade="F-";
                }
                else
                {
                outputGradeSingle=(studentMark/10);
                outputGrade= String.valueOf(outputGradeSingle);
                }
        break;
        default: outputGrade="Not a valid amount of assignments, range is between 0 and 5";
        break;
        }
        return outputGrade;
        }
}
4

1 回答 1

1

It is because you are assigning a double to an int and you get a possible loss of precision error. Just explicitly cast the result int and you should be fine (assuming you are ok with the decimals being truncated):

outputGradeSingle = (int) (studentMark / 10);

Note: you state

mark=79 then outputGrade string = "9"

if mark is 79, outputGrade will be 7 with your code.

于 2013-05-20T15:29:07.353 回答