-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOPsMinAndMaxChallenge023.java
45 lines (36 loc) · 1.25 KB
/
OOPsMinAndMaxChallenge023.java
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
import java.util.Scanner;
public class OOPsMinAndMaxChallenge023 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int minNum = 0;
int maxNum = 0;
int count = 0;
System.out.println("Enter numbers to find minimum and maximum. If you want to exit, enter an alphabet.");
boolean choice = true;
while (choice) {
String inputNum = scan.nextLine();
try {
int inputNumber = Integer.parseInt(inputNum);
if (count == 0 || inputNumber < minNum) {
minNum = inputNumber;
}
if (count == 0 || inputNumber > maxNum) {
maxNum = inputNumber;
}
count++;
} catch (NumberFormatException nfe) {
// System.out.println("Number not entered.");
// System.out.print("Do you want to continue? Enter yes or no: ");
// String ans = scan.nextLine();
// choice = ((ans == "yes" || ans == "y" || ans == "YES" || ans == "Y") ? true : false);
// if (choice == true) {
// System.out.print("Enter a valid number: ");
// }
break;
}
}
System.out.println("Smallest number entered: " + minNum);
System.out.println("Largest number entered: " + maxNum);
scan.close();
}
}