虽然与哨兵循环的Java

虽然与哨兵循环的Java

问题描述:

我写使用下面的程序,而哨兵循环。
我遇到的问题包括:
(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

你没有关注你的伪代码,这是你想要什么:

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

@ David SN:非常有帮助,非常感谢你。增量建议以及取出for循环完全有帮助。 @ GhostCat:谢谢,取出for循环有助于简化这个程序。

我跑进与被发现的smallest_score正确的值方案的另一个问题。由于哨兵在技术上比最小得分的初始值以下(即smallest_score = 0 & sentinel = -1),该smallest_score变量的结果等于其初始值,0我试图smallest_score变量分配一个真正大的价值,它似乎工作。只要输入的exam_score值不超过smallest_score变量的值,代码似乎遵循程序规则。因此,相同的代码如以前,但分配smallest_score变量出奇大的值,如99999999999.

谢谢你这么多给出的所有帮助。非常感谢,我期待着向大家学习。