-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathLargestAndSmallest.java
More file actions
58 lines (46 loc) · 1.52 KB
/
Copy pathLargestAndSmallest.java
File metadata and controls
58 lines (46 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.Scanner;
/**@author Lemuel M. Uhuru
* @Date July 12, 2013
* Book Title: From Control Structures through Objects by Tony Gaddis
* Page: 268 Challenge 10: Largest and Smallest
*
* @Description Write a program with a loop that lets the user enter a series
* of integers. The user should enter -99 to signal the end of the series. After
* all the numbers have been entered, the program should display the largest and
* smallest numbers entered.
*/
public class LargestAndSmallest {
public static void main(String[] args) {
// Store user input
int value;
// Smallest value
int smallest;
// Larget value
int largest = 0;
// Create Scanner object
Scanner input = new Scanner(System.in);
// Display program instructions
System.out.println("Instructions: The computer will prompt you for a");
System.out.println("number value each time until you enter the number" + "\'\n-99'");
// Prompt user for input
System.out.print("Enter a number: ");
value = input.nextInt();
// Assign first value as smallest
smallest = value;
// Construct while loop to prompt user for input using a sentinel condition.
while(value != -99){
System.out.print("Enter a number: ");
value = input.nextInt();
if(value != -99){
if (value > smallest){
largest = value;
}
else{
smallest =value;
}
}
}
// Display largest and smallest value
System.out.println("Smallest Value: " + smallest + "\nLargest Value: " + largest);
}
}