tags:

views:

93

answers:

3

This is an example straight out of my text book, and I can't get it to end the loop.

import java.util.Scanner;

public class Play {

    System.out.println("Enter scores for all students.");
    System.out.println("Enter a negative number after");
    System.out.println("you have entered all the scores.");

    Scanner keyboard = new Scanner(System.in);
    double max = keyboard.nextDouble();
    double min = max;
    double next = keyboard.nextDouble();
    while(next >= 0)
    {
        if(next > max)
            max = next;
        else if (next < min)
            min = next;
        next = keyboard.nextDouble();

    }
    System.out.println("The highest score is " + max);
    System.out.println("The lowest score is " + min);
}
A: 

Your condition says next >= 0, it should be next > 0. Are you supposed to enter a negative value to finish entering values?

KennyN
A: 

I fixed the demo for you (see below).

He was using while when he should use do while.

The demo states that you use a negative number to exit the loop (below zero).

 public static void main(String[] args) {
    System.out.println("Student score entry demo:");
    System.out.println("NB: Enter a negative number to finish.\n");
    Scanner keyboard = new Scanner(System.in);
    double max = -1;
    double min = max;
    double score;
    do {
      System.out.println("Enter student score: ");
      score = keyboard.nextDouble();
      if (score > max) {
        max = score;
      } else if (min < 0 || score < min && score >= 0) {
        min = score;
      }
    } while (score >= 0);
    // Output results
    System.out.println("The highest score was: " + max);
    System.out.println("The lowest score was: " + min);
  }

Output:

Student score entry demo:

NB: Enter a negative number after you have entered all the scores to finish.

Enter student score:
22

Enter student score:
33

Enter student score:
44

Enter student score:
55

Enter student score:
11

Enter student score: -3

The highest score was: 55.0 The lowest score was: 11.0

Syntax
every do-while can be written as a while and vice-versa. I don't understand how this made the difference.
Sagar V
That helps a lot, but can you explain your logic behind setting max = -1?
Breedly
@Sagar V: yes, but making it a do while means you don't need to have the additional pre-loop read which is ugly.
Syntax
@Breedly -1 is just a sentinel value, I have no strong reason for having started at -1; I'm using it to check if min had been set now that the pre-loop read has been removed. TBH I prefer the use of Double.MIN/MAX as used in Hila's answer.
Syntax
A: 

Here are my 0.02$. The logic is explained in the comments.

public static void main(String[] args) {

// Print instructions
System.out.println("Enter scores for all students.");
System.out.println("Enter a negative number after");
System.out.println("you have entered all the scores.");

Scanner keyboard = new Scanner(System.in);

// Init min and max
// Any grade will be lower than the current min grade 
// (which is the higest possible value a double can hold),
// and higher than the current max grade.
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;

// Read first grade. Allow the user to terminate the loop
// without entering grades
double next = keyboard.nextDouble();
while(next >= 0)
{
    if(next > max)
        max = next;

    // Removed else-if. This ensures the first grade is
    // initialized correctly
    if (next < min)
        min = next;
    next = keyboard.nextDouble();

}

// Make sure we got some grades
if (max == Double.MIN_VALUE && min == Double.MAX_VALUE) {
    System.out.println("No scores entered. Your students are lazy.");
} else {
    System.out.println("The highest score is " + max);
    System.out.println("The lowest score is " + min);
}

}

Try to enter -1 as the only grade, and a list of grades followed by -1, see what you get.

Hila
I like the use of Double.MIN/MAX but I think removing the pre-loop read would be better.
Syntax
@Syntax I wanted to allow the user to be able to terminate the loop without entering any grades. It really depends on how you want your application to behave, and if you do want to allow this, well - in that case it's a matter of style.
Hila