私は、whileループを使って以下のプログラムを記述しています。
私が持っている問題は次のとおりです:
(1) "最小スコア"を得る方法を発見するには、値がセンチネルと同じでなければなりません。
(2)整数が1つだけ入力された場合、「最大得点」と「最小得点」を取得します。while Sentinels for Javaのwhileループ
ご協力いただきますようお願い申し上げます。以下は、プログラムの詳細です:
//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ループを確認するには「場合」を使用することになっていません。 exitやbreakを使わないでください。また、センチネル値と2つの読み込みステートメントを必ず使用します。このプログラムのコードは、次のアルゴリズムのようになります。
read data // first data
while not last data{
process data
read data
}
そして、これはあなたが持っているものです:あなたはあなたの擬似コードを、次のされていません
read data // first data
while not last data {
process data
read data
}
あなたのコードは非常に混乱しています。実行する予定の**ループ操作数**を定義するために、 "exam_score"と呼ばれるものを使用しないでください**。おそらくそれはあなたの問題です - その状態では全く感覚がありません。 GhostCat