Advertisement
krystation

FindRange

Jul 18th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. /*
  2.  * File: FindRange.java
  3.  * --------------------
  4.  * This program is a stub for the FindRange problem, which finds the
  5.  * smallest and largest values in a list of integers.
  6.  */
  7.  
  8. import acm.program.*;
  9.  
  10. public class FindRange extends ConsoleProgram {
  11.  
  12.     /* SENTINEL = 0 */
  13.    
  14.     private  static final int SENTINEL=0;
  15. /* this program will generte a list of integers given by the user, and find the smallest and largest integers */   
  16.     public void run() {
  17.         int smallestNumber=1000000000;
  18.         int largestNumber=-1000000000;
  19.         while (true){
  20.             int Int=readInt("?:");
  21.             if (Int==SENTINEL) break;
  22.             if (Int>largestNumber){
  23.                 largestNumber=Int;
  24.             }
  25.             if (Int<smallestNumber){
  26.                 smallestNumber=Int;
  27.             }
  28.         }
  29.         if (largestNumber==-1000000000){
  30.             if (smallestNumber==1000000000){
  31.                 println("No solution!");
  32.             }
  33.         } else {
  34.             println("The largest integer is:"+largestNumber+".");
  35.             println("The smallest integer is:"+smallestNumber+".");
  36.         }  
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement