簡體   English   中英

有人可以向我解釋一下哨兵在Java中的作用嗎? 或者它是如何工作的?

[英]Can someone explain to me what a sentinel does in Java? Or how it works?

我試圖了解哨兵是什么或它如何與該計划一起工作。 無論如何,這是我想要了解的代碼塊。 我知道這是一個哨兵控制循環,但我不知道它是做什么的。

private static final int SENTINEL = -999

據我所知,通過使用負整數表示序列的結束。 但它是如何做到的呢? 哦,我該如何初始化哨兵? 它已經初始化了嗎?

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

謝謝,麻煩您了! 我正在學習如何用Java編寫代碼

“哨兵”沒有什么特別之處。 它只是您選擇的任何常量,它不是數據集中的合法值,因此您可以使用它來標記序列的結尾。 例如,如果團隊的盒子分數永遠不會小於零,則可以將-999(或任何其他負值)用作中斷/結束標記。

通常,有更好,更清潔的方法來做到這一點。

通過simple 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™如何編程(早期對象),第十版

Sentinel值用於避免在循環內進行額外檢查。

例如,當在未排序列表中搜索特定值時,將將每個元素與該值進行比較,並在找到相等時循環終止; 但是,為了處理不存在該值的情況,必須在完成搜索失敗的每個步驟之后進行測試。 通過將搜索到的值附加到列表的末尾,不再可能進行不成功的搜索,並且內循環中不需要顯式終止測試; 之后必須仍然決定是否找到了真正的匹配,但是這個測試只需要執行一次而不是每次迭代。

請參閱https://en.wikipedia.org/wiki/Sentinel_value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM