public String toString()
{
String str;
str = "The test scores in descending order are \n";
for(int i = 0; i < data.length; i++)
{
str = str + data[i] + " ";
}
str = str + "\nThe average is " + mean;
return str;
}
这段Java代码按降序返回我的代码得分。但是,让我困惑的是'str'的返回方式。我的理解是'str'的返回值为(str +“\nThe average is”+ mean)。因为这是我最后更新的'str'值,它将更新'str'为“ The test scores in descending order are”First, Second the loop, then last "str + "\nThe average is " + mean"。所以最终即使我们多次更新了'str',实际上打印出来的'str'只会是“ str +“\nThe average is”+ mean”。请解释为什么程序最终会打印出:
按降序排列的测试分数为 70 80.......... (元素值)
(然后返回平均值信息)