next up previous
Next: Loop scheme for characteristic Up: Unit 06 Previous: Loop scheme for characteristic

Loop scheme for characteristic values in a set: maximum of a non-empty set

Let us consider the problem of finding the maximum of a set of reals in input. Assume that:

String s;       // current string in input
double r;       // current real
double max;     // current maximum

s = JOptionPane.showInputDialog("Input a real");
max = Double.parseDouble(s);
s = JOptionPane.showInputDialog("Input a real");
while (s != null) {
  r = Double.parseDouble(s);
  if (r > max)
    max = r;
  s = JOptionPane.showInputDialog("Input a real");
}

System.out.println("massimo = " + max);

Note: Since we know that the set of integers cannot be empty, we can read the first real and use it to initialize the maximum value with it before we start the loop.


next up previous
Next: Loop scheme for characteristic Up: Unit 06 Previous: Loop scheme for characteristic