3

I am trying to understand what sentinel is or how it works with the program. Anyways this is the block of code I am trying to understand. I know it is a sentinel control loop, but I don't know what it does.

private static final int SENTINEL = -999

From what I have Googled is that by having a negative integer it indicates the end of a sequence. But how does it do that? Oh, and how do I initialize the sentinel? Is it already initialized?

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;
} 

Please and Thank you! I am learning how to code with Java

4

3 回答 3

8

“哨兵”没有什么特别之处。它只是您选择的任何不是数据集中合法值的常数,因此您可以使用它来标记序列的结束。例如,如果一支球队的得分永远不会小于零,则可以使用 -999(或任何其他负值)作为休息/结束标记。

通常,有更好、更清洁的方法来做到这一点。

于 2014-02-09T23:28:31.580 回答
2

为了理解sentinel-controlled repetition让我们看一个simple例子,

import java.util.Scanner; // program uses class Scanner

public class ClassAverage
{
  public static void main(String[] args)
  {
     // create Scanner to obtain input from command window
     Scanner input = new Scanner(System.in);

     // initialization phase
     int total = 0; // initialize sum of grades
     int gradeCounter = 0; // initialize # of grades entered so far

     // processing phase
     // prompt for input and read grade from user    
     System.out.print("Enter grade or -1 to quit: ");
     int grade = input.nextInt();                    

     // loop until sentinel value read from user
     while (grade != -1)
     {
        total = total + grade; // add grade to total
        gradeCounter = gradeCounter + 1; // increment counter

        // prompt for input and read next grade from user
        System.out.print("Enter grade or -1 to quit: "); 
        grade = input.nextInt();                         
     }

     // termination phase
     // if user entered at least one grade...
     if (gradeCounter != 0)
     {
        // use number with decimal point to calculate average of grades
        double average = (double) total / gradeCounter;                

        // display total and average (with two digits of precision)
        System.out.printf("%nTotal of the %d grades entered is %d%n",
           gradeCounter, total);
        System.out.printf("Class average is %.2f%n", average);
     }
     else // no grades were entered, so output appropriate message
        System.out.println("No grades were entered");
  }
} // end class ClassAverage

现在让我们运行它

Enter grade or -1 to quit: 97
Enter grade or -1 to quit: 88
Enter grade or -1 to quit: 72
Enter grade or -1 to quit: -1

Total of the 3 grades entered is 257
Class average is 85.67

在哨兵控制的循环中,提示应该提醒用户注意哨兵。这是一个很好的做法

参考:Java™ 如何编程(早期对象),第十版

于 2017-02-08T14:19:09.317 回答
1

哨兵值用于避免循环内的额外检查。

例如,在未排序列表中搜索特定值时,每个元素都将与该值进行比较,当找到相等时循环终止;但是,为了处理不存在该值的情况,还必须在每个步骤之后测试是否未成功完成搜索。通过将搜索到的值附加到列表的末尾,不再可能搜索不成功,并且在内循环中不需要显式终止测试;之后,仍然必须确定是否找到了真正的匹配,但是这个测试只需要执行一次,而不是在每次迭代时执行。

参考https://en.wikipedia.org/wiki/Sentinel_value

于 2019-01-26T23:13:36.470 回答