Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. /*
  2. * File: FindRange.java
  3. */
  4.  
  5. import acm.program.*;
  6.  
  7. public class FindRange extends ConsoleProgram {
  8.  
  9. /* Specifies the value of the sentinel */
  10. private static final int SENTINEL = 0;
  11.  
  12. public void run() {
  13. println("This program finds the largest and smallest numbers entered.");
  14. println("Enter values, one per line, using " + SENTINEL + " to signal the end of the list.");
  15. int min = 0;
  16. int max = 0;
  17. int count = 0;
  18. while (true) {
  19. int value = readInt(" ? ");
  20. if (value == SENTINEL) break;
  21. count++;
  22. if (count == 1){
  23. min = value;
  24. max = value;
  25. }
  26. min = Math.min(min, value);
  27. max = Math.max(max, value);
  28. }
  29. if (count>0){
  30. println("Smallest: " + min);
  31. println("Largest: " + max);
  32. } else {
  33. println("No values were entered.");
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement