-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminMaxNum.java
More file actions
29 lines (28 loc) · 910 Bytes
/
minMaxNum.java
File metadata and controls
29 lines (28 loc) · 910 Bytes
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
import java.util.Scanner;
public class minMaxNum{
public static void main (String[] args){
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
String inputVar = "";
Scanner input = new Scanner(System.in);
System.out.println("Please enter a series of integers (q to exit)");
while(!(inputVar.equals("q"))){
inputVar = input.next();
if (!inputVar.equals("q")){
if (inputVar.matches("^-?\\d+$")){
if (Integer.parseInt(inputVar) < min){
min = Integer.parseInt(inputVar);
}
if (Integer.parseInt(inputVar) > max){
max = Integer.parseInt(inputVar);
}
}
else{
System.out.println("Not a valid integer.");
}
}
}
input.close();
System.out.println("\nThe smallest value is: " + min + "\nThe largest value is: " + max);
}
}