0
How is return used in java. This code tells me that the return I used is error .why?
public class Program { public static void main(String[] args) { int x=50; int y=70; Math.max(x,y); if(x<y){ System.out.println(x); } else{ return y; } } }
2 Antworten
+ 2
Tony Jadesola
First thing main method has return type void means main method doesn't return anything.
2nd thing you have to assign Math.max(x, y) to a variable because max has return type which return something.
3rd thing if you want to get max value through comparison then you have to create a method which should have return type int and should return value in if and else both case.
So here is user defined method:
int getMaxValue(int x , int y) {
if (x > y) {
return x;
} else {
return y;
}
}
Now call this method and paas arguments x and y.
Remember you have to assign returned value to a new variable or you can directly print method.
using inbuilt method:
int result = Math.max(x, y);
+ 3
main method in java is void return type. Don't expecting anything to return.
Then return y; invalid.
Where its return, to whom?
Tony Jadesola What are you trying by that line?