Error: Program.java:16: error: '.class' expected return int minor;
What's going on?
public class Programa {
public static void main(String[] args) {
int x = 23, y = 45;
int menor=esMenor(x, y);
if( menor == x) {
System.out.println("x es menor");
}
}
public static void esMenor(int a, int b) {
if (a < b) {
int menor = a;
} else if (a >= b) {
int menor = b;
}
return int menor;
}
}
You have a number of problems with your method
esMenor
:First you want it to return a
int
for the same thing when declaring your method, you should not do itvoid
( The word void indicates that the method in question does not return any value ) if notint
, say:So you can get a
int
from the method call in themain
On the other hand you have an error in the declaration of the variable
menor
inside the methodesMenor()
In other words, every time you are going to return the
menor
it will return the empty value since when you declare the variable again, it is created and does not take any value.The correct way would be
The ideal is to declare the variable at the beginning, with value 0 since in the case of not fulfilling the conditions declared below, when returning it will not give an error.
As Riaven said, you should never put Void if you want to return a value, since, so to speak, everything you put in a void method is like a tube with only 1 exit, which is where everything enters but does not leave, instead when you do a normal method, you can leave everything that happened inside and go to another place, I hope it will serve you for your future projects.