我试图了解哨兵是什么或它如何与该计划一起工作.无论如何,这是我想要了解的代码块.我知道这是一个哨兵控制循环,但我不知道它是做什么的.
private static final int SENTINEL = -999
Run Code Online (Sandbox Code Playgroud)
据我所知,通过使用负整数表示序列的结束.但它是如何做到的呢?哦,我该如何初始化哨兵?它已经初始化了吗?
public static int gameScore(int[] teamBoxScore) { //This is telling me the name of an array
int output = 0;
for (int v : teamBoxScore){ //I know that this the values of the array will be stored in the variable "v".
if (v !=SENTIENL) {//
output += v;
}
}
return output;
}
Run Code Online (Sandbox Code Playgroud)
谢谢,麻烦您了!我正在学习如何用Java编写代码
"哨兵"没有什么特别之处.它只是您选择的任何常量,它不是数据集中的合法值,因此您可以使用它来标记序列的结尾.例如,如果团队的盒子分数永远不会小于零,则可以将-999(或任何其他负值)用作中断/结束标记.
通常,有更好,更清洁的方法来做到这一点.
| 归档时间: |
|
| 查看次数: |
27360 次 |
| 最近记录: |