嘿我无法弄清楚如何将类型作为String返回,因为我已将outputGrade定义为String但我正在尝试计算单个数字。例如mark = 79然后输出字符串=“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;
}
}
答案 0 :(得分:1)
这是因为您要为double
分配int
,并且您收到possible loss of precision
错误。只是显式地转换结果int
,你应该没问题(假设你可以截断小数):
outputGradeSingle = (int) (studentMark / 10);
注意:你说明了
mark = 79然后输出字符串=&#34; 9&#34;
如果mark
为79,outputGrade
将为您的代码7
。