package com.codegym.task.task02.task0217;
/*
Minimum of four numbers
*/
public class Solution {
public static int min(int a, int b, int c, int d) {
//write your code here
int min;
if (a <= b & a <= c & a <= d)
min = a;
else if (b <= a & b <= c & b <= d)
min = b;
else if (c <= a & c <= b & c <= d)
min = c;
else if ( d <= a & d <= c & d <= b)
min = d;
return min;
}
public static int min(int a, int b) {
//write your code here
if (a < b)
return a;
else
return b;
}
public static void main(String[] args) throws Exception {
System.out.println(min(-20, -10));
System.out.println(min(-20, -10, -30, -40));
System.out.println(min(-20, -10, -30, 40));
System.out.println(min(-40, -10, -30, 40));
}
}
variable min might not have been initialized return min; ^ 1 error. how do you initialized min?
Resolved
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Lisa
21 September 2021, 20:32
The compiler sees an if and three following else if conditions. If none of these conditions evaluate to true, then min won't get initialized. That's what the compiler is complaining about. Restructure the if - else if cascade so that min gets initialized in every code path or initialize it when you declared it with some made up value (0?).
Yeah, yeah.. that's all the magic 🤪😜 crazy
+3