我寫使用下面的程序,而哨兵循環。
我遇到的問題包括:
(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
}
您的代碼非常混亂。您應該**不要**使用稱爲「exam_score」的內容來定義您打算運行的**循環操作數**。可能這就是你的問題 - 在這種情況下,我絕對沒有意識到我 GhostCat