public class Solution {
public static void main(String[] args) throws Exception {
Scanner sc1 = new Scanner(System.in);
int x = sc1.nextInt();
int y = sc1.nextInt();
int z = sc1.nextInt();
int w = sc1.nextInt();
int r = sc1.nextInt();
int d = min(x, y, z, w, r);
System.out.println("Minimum =" + d);
}
public static int min(int a, int b, int c, int d, int e) {
if (a > b && a > c && a > d && a > e) {
return a;
}
if (b > a && b > c && b > d && b > e) {
return b;
}
if (c > a && c > b && c > d && c > e) {
return c;
}
if (d > a && d > b && d > c && d > e) {
return d;
}
if (e > a && e > b && e > c && e > d) {
return e;
}
}
}
Could someone check What is wrong with my code?
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Vadim
11 May 2020, 13:57
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(reader.readLine());
int b = Integer.parseInt(reader.readLine());
int c = Integer.parseInt(reader.readLine());
int d = Integer.parseInt(reader.readLine());
int e = Integer.parseInt(reader.readLine());
int min = min(a, b, c, d, e);
System.out.println("Minimum = " + min);
}
public static int min(int a, int b, int c, int d, int e) {
int m2 = a < b ? a : b;
m2 = m2 < c ? m2 : c;
m2 = m2 < d ? m2 : d;
m2 = m2 < e ? m2 : e;
return m2;
}
0
Atakan Isik
8 January 2020, 09:20
0
Chadd McLaughlin
2 January 2020, 13:38
Your approach is ok...however, it is the minimum you are seeking, not the max...you are returning the highest value...you should just need to reverse your ">" sign...however, you also need to account for "=" condition.If the values are equal, you can return either value.In my approach, I compared only 2 values at a time and found the lesser of the 2 and then moved to the next parameter and so on until the list was exhausted.
0
Antali András
2 January 2020, 12:02
1) It is not the minimum, but the maximum value you want the method to return with.
2) What about the case, if you have 2 or more equal maximum value? Say, 5,5,1,2,3. You have 2 pieces of fives. None of them is greater than the other numbers, so the procedure fails.
3) I think your approach is too complicated. I suggest you to try this: take an int array to store 5 numbers instead of creating 5 individual variables, and take an int "min" variable as well. Start a "for" cycle through the array: if the cycle variable is 0, then set "min" to the value of array[0]. After that, for all the four further elements: check if the array[i] is less than the "min". If it is, then set the "min" to this new value. After the cycle, return with this "min".
0