My first thought was to do something like this and it (of course) did not work. The question is... is there a way to make something like this work in a slightly different way?
public class Solution {
public static int min(int a, int b, int c) {
int m1;
int m2;
if (a < b){
m1 = a;}
else {m1 = b;}
return m1;
if (m1 < c){
m2 = m1;}
else {m2 = c}
return m2;
}
public static void main(String[] args) throws Exception {
System.out.println(min(1, 2, 3));
System.out.println(min(-1, -2, -3));
System.out.println(min(3, 5, 3));
System.out.println(min(5, 5, 10));
EDIT:
I have taken the advice and updated the code removing the first return statement and adding the missing ; - but it is not working.
package com.codegym.task.task02.task0216;
/*
Minimum of three numbers
*/
public class Solution {
public static int min(int a, int b, int c) {
int m1;
int m2;
if (a < b){
m1 = a;}
else {
m1 = b;}
if (m1 < c){
m2 = m1;}
else {m2 = c;}
return m2;
}
}
}
}
public static void main(String[] args) throws Exception {
System.out.println(min(1, 2, 3));
System.out.println(min(-1, -2, -3));
System.out.println(min(3, 5, 3));
System.out.println(min(5, 5, 10));
}
}