package com.codegym.task.task07.task0721;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Min and max in arrays
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int[] numbers = new int[20];
int maximum = numbers[0];
int minimum = numbers[0];
for (int i = 0; i < 20; i++) {
numbers[i] = Integer.parseInt(reader.readLine());
}
for (int i = 0; i < numbers.length; i++) {
if (maximum < numbers[i]) {
maximum = numbers[i];
}
if (minimum > numbers[i]) {
minimum = numbers[i];
}
}
System.out.print(maximum + " " + minimum);
}
}
where is the mistake?
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Karolis Garšva
8 July 2021, 20:23
yes my output is 111 and -150. So whats the problem?
0
Thomas
8 July 2021, 20:59solution
At that time you initialize minimum and maximum you haven't filled the array with values and therfore minimum and maximum get assigned 0.
+1
Karolis Garšva
8 July 2021, 21:12
Thanks mate, the problem was not finding min and max. if I would look at the code for another hour I would figured it out my self :D
0
Guadalupe Gagnon
8 July 2021, 19:50
Try entering 20 negative numbers. For example:
-1, -2, -3, -4, -5, ... -20
The correct output would be:
"-1 -20"
+1