Between Stanford CS106A and 'Art and Science of Java' (the course textbook.

Just showing people some of the code, not all of it though. Only when I feel like it.

Monday, July 29, 2013

Code for letting the user enter a bunch of numbers, then when 0 is hit, stops, reads back the highest and lowest numbers that were entered.
1:  /*  
2:   * File: FindRange.java  
3:   * Name:  
4:   * Section Leader:  
5:   * --------------------  
6:   * This file is the starter file for the FindRange problem.  
7:   */  
8:  import acm.program.*;  
9:  public class FindRange extends ConsoleProgram {  
10:       public void run() {  
11:            int number, small = Integer.MAX_VALUE, large = 0;  
12:            println("This program finds the largest and smallest numbers.");  
13:            number = readInt("?");  
14:            while (true) {  
15:                 number = readInt("?");  
16:                 if (number == 0) {  
17:                      break;  
18:                 }  
19:                 if (number > large) {  
20:                      large = number;  
21:                 }  
22:                 if (number < small) {  
23:                      small = number;  
24:                 }  
25:            }  
26:            println("smallest was " + small + " and largest was " + large);  
27:       }  
28:  }  

No comments:

Post a Comment