Skip to content

Instantly share code, notes, and snippets.

@kenechiokolo
Created March 16, 2015 18:56
Show Gist options
  • Save kenechiokolo/7da5f16947c9d06af5dd to your computer and use it in GitHub Desktop.
Save kenechiokolo/7da5f16947c9d06af5dd to your computer and use it in GitHub Desktop.
CS106A: Assignment 2.5
/*
* File: FindRange.java
* Name:
* Section Leader:
* --------------------
* This file is the starter file for the FindRange problem.
*/
import acm.program.*;
public class FindRange extends ConsoleProgram {
private static final int SENTINEL = 0;
public void run() {
displayWelcomeMessage();
findRange();
}
// displays welcome message
private void displayWelcomeMessage() {
println("This program displays the smallest and largest numbers.");
}
// takes user input and finds smallest and largest numbers
private void findRange() {
int firstNumber = readInt("?");
// if first number entered is not equal to SENTINEL then program continues
if (firstNumber != SENTINEL) {
// initially, both the smallest and largest numbers are assigned the value of the first entered integer
int smallestNumber = firstNumber;
int largestNumber = firstNumber;
while (true) {
int value = readInt("?");
// if the SENTINEL is entered then the program exits this code block
if (value == SENTINEL) break;
// if the smallest number is greater than the user input, it is assigned the value of the user input
if (smallestNumber > value) {
smallestNumber = value;
}
// if the largest number is smaller than the user input, it is assigned the value of the user input
if (largestNumber < value) {
largestNumber = value;
}
}
println("smallest number: "+ smallestNumber +"");
println("largest number: "+largestNumber+"");
} else {
// for special case when user inputs 0 as the first integer
println("No values have been entered. (0 does not count!)");
findRange();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment