2016-10-04 30 views
0

私は、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 
} 
+1

あなたのコードは非常に混乱しています。実行する予定の**ループ操作数**を定義するために、 "exam_score"と呼ばれるものを使用しないでください**。おそらくそれはあなたの問題です - その状態では全く感覚がありません。 GhostCat

答えて

1

、これはあなたが望むものである

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に設定し、2つのスコアを読み取ったことになります。そのそれぞれの読み取りの後に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

ヒント:そこにループを使用する意味はありません(とにかく使用されていません)。だから、ちょうど1つの(while)ループに行かないのはなぜですか?あなたはその答えに言及したいかもしれません。 – GhostCat

0

@ David SN:Super helpful、ありがとうございます。増分の推薦とforループの取り出しは完全に助けになりました。 @ GhostCat:ありがとう、forループを取り出して、このプログラムを簡単にしました。

私は、smallest_scoreに適切な値を見つけていたプログラムで別の問題が発生しました。小さな値の初期値(つまり、smallest_score = 0 & sentinel = -1)よりも技術的に小さいので、smallest_score変数の結果は初期値0になります。実際に大きな値をsmallest_score変数に割り当てようとしましたが、作業。入力されたexam_scoreの値がsmallest_score変数の値を超えない限り、コードはプログラムのルールに従っているようです。したがって、前と同じコードですが、smallest_score変数には99999999999のような非常に大きな値を割り当てます。

ありがとうございました。大変感謝しており、私は皆さんから学ぶことを楽しみにしています。