2016-10-04 85 views
0

我寫使用下面的程序,而哨兵循環。
我遇到的問題包括:
(1)發現如何獲得「最小得分」進入不等於哨兵值。 (2)只輸入一個整數時,獲得「最大分數」和「最小分數」。雖然與哨兵循環的Java

任何幫助將不勝感激。以下是該計劃的細節:

//Write a program that inputs a series of exam scores as integers. 
//The number of scores is not limited. 
//Print the number of scores entered and the largest and smallest score entered. 
//Use a sentinel to terminate the input. 
    //Input: Series of exam scores as integers. 
    //Output: Number of scores entered and the largest and smallest score entered. 

import java.util.Scanner; 

public class hwk_6_1 { 
    public static void main(String [] args) { 

     int exam_score; 
     int number_of_scores = 0; 
     int i; 

     Scanner keyboard = new Scanner(System.in); 
     System.out.print("Enter exam score or -1 to end: "); 
     exam_score = keyboard.nextInt(); 

     int largest_score = 0; 
     int smallest_score = 0; 

     while(exam_score != -1) { 
      for(i = 0; i < i + exam_score; i++) { 
       System.out.print("Enter exam score or -1 to end: "); 
       exam_score = keyboard.nextInt(); 
       number_of_scores = i; 

       if(exam_score > largest_score) { 
        largest_score = exam_score; 
       } 
       if(exam_score < smallest_score) { 
        smallest_score = exam_score; 
       } 
      } 
     } 
     System.out.println("The number of scores entered: " + (number_of_scores + 1) + "."); 
     System.out.println("Largest score: " + largest_score + "."); 
     System.out.println("Smallest score: " + smallest_score + "."); 
    } 
} 

注:我們不應該使用「如果」來檢查哨,只有while循環;不要使用退出或休息。另外,我們要確保使用一個標記值和兩個讀取語句。代碼對這一計劃應該類似於下面的算法:

read data // first data 
while not last data { 
    process data 
    read data 
} 
+1

您的代碼非常混亂。您應該**不要**使用稱爲「exam_score」的內容來定義您打算運行的**循環操作數**。可能這就是你的問題 - 在這種情況下,我絕對沒有意識到我 GhostCat

回答

1

你沒有關注你的僞代碼,這是你想要什麼:

read data // first data 
while not last data{ 
process data 
read data 
} 

這是你擁有的一切:

read data // first data 
while not last data{ 
read data // <-- you are reading again and ignoring the first value 
process data 
} 

你需要處理的數據,並在循環的閱讀正文之後的下一個數據跟蹤你的目標僞代碼。

而且,這種情況可以得到改善:

for(i = 0; i < i + exam_score; i++) 

你並不需要在條件使用i,你只需要檢查,如果考試成績是-1,所以它可能是:

for(i = 0; exam_score != -1; i++) 

還有一個分數計數問題,您將numer_of_scores設置爲i。在第一次迭代之後,您將值設置爲0,並且您已經閱讀了兩個分數。每次閱讀後最好使用number_of_scores++,根本不要使用i

public static void main(String [] args) 
{ 
    int exam_score; 
    int number_of_scores = 0; 

    Scanner keyboard = new Scanner(System.in); 
    System.out.print("Enter exam score or -1 to end: "); 
    exam_score = keyboard.nextInt(); 

    int largest_score = 0; 
    int smallest_score = 0; 

    while(exam_score != -1) 
    { 
     number_of_scores++; 

     if(exam_score > largest_score) 
     { 
      largest_score = exam_score; 
     } 
     if(exam_score < smallest_score) 
     { 
      smallest_score = exam_score; 
     } 
     System.out.print("Enter exam score or -1 to end: "); 
     exam_score = keyboard.nextInt(); 
    } 
    System.out.println("The number of scores entered: " + number_of_scores + "."); 
    System.out.println("Largest score: " + largest_score + "."); 
    System.out.println("Smallest score: " + smallest_score + "."); 
} 
+1

提示:在那裏使用for循環是沒有意義的(我沒有用過);那麼爲什麼不去一個(while)循環呢?!你可能想在你的答案中提到。 – GhostCat

0

@ David SN:非常有幫助,非常感謝你。增量建議以及取出for循環完全有幫助。 @ GhostCat:謝謝,取出for循環有助於簡化這個程序。

我跑進與被發現的smallest_score正確的值方案的另一個問題。由於哨兵在技術上比最小得分的初始值以下(即smallest_score = 0 & sentinel = -1),該smallest_score變量的結果等於其初始值,0我試圖smallest_score變量分配一個真正大的價值,它似乎工作。只要輸入的exam_score值不超過smallest_score變量的值,代碼似乎遵循程序規則。因此,相同的代碼如以前,但分配smallest_score變量出奇大的值,如99999999999.

謝謝你這麼多給出的所有幫助。非常感謝,我期待着向大家學習。