1int[] arr = {2, 3, 4, 5, 6, 7, 8, 9,};
2 int max = arr[0]; // max tanimliyorsun
3 int min = arr[0]; // min tanimliyorsun
4
5 for (int i = 0; i < arr.length; i++) {
6 if (arr[i] > max){
7// array's index are compared with each other,
8// whichever is greater will be assigned to max
9 max = arr[i];
10 }
11 if(arr[i] < min){
12//array's index are compared with each other,
13// whichever is smaller will be assigned to min
14 min = arr[i];
15 }
16 }
17 System.out.println(max); // 9
18 System.out.println(min); // 2
19
1 Scanner input = new Scanner(System.in);
2 // Minimum And Maximum
3 int count = 0;
4 int min = 0;
5 int max = 0;
6 boolean bugSolved = true;
7 /* or we can use :
8 int min = Integer.MAX_VALUE;
9 int max = Integer.MIN_VALUE;
10 */
11
12 while (true){
13 int cnt = count++;
14 System.out.print("Enter Number #"+(cnt+1)+": ");
15 boolean isValid = input.hasNextInt();
16 if(isValid){
17 int num = input.nextInt();
18 /* if (bugSolved){
19 bugSolved = false;
20 min = num;
21 max = num;
22 } # Just remove this condition and
23 boolean (bugSolved) at the top, if you use
24 int min = Integer.MAX_VALUE and int max =
25 Integer.MIN_VALUE */
26 if (num < min) {
27 min = num;
28 }else if (num > max){
29 max = num;
30 }
31 }else{
32 System.out.println("Invalid input..");
33 break;
34 }
35 input.nextLine();
36 }
37 System.out.println("Min Number : " + min);
38 System.out.println("Max Number : " + max);