Void problem

Joined
Mar 20, 2023
Messages
1
Reaction score
0
Hello Everyone I have a problem with the void stuff in java I do not understand exactly what it does I did this code and I put public static void in the Mayor method but java told me that it can not be used there now this code works but I do not understand exactly how the void statemente works, any help will be so useful thank you so much

package pruebas.pruebasbuenas;
import java.util.Scanner;
public class granja {

public static void main(String[] args){
Scanner input = new Scanner(System.in);
//atributos//
int n = input.nextInt();
int j = input.nextInt();

//Condición de igualdad//
if(n==j){
System.out.println("ambos números son iguales");
}
else{
System.out.println(Mayor(n,j));
}






}

public static int Mayor(int n,int j){
if(n>j){
return n;
}
else{
return j;
}

}
}
 
Joined
Apr 25, 2017
Messages
248
Reaction score
32
You don't have to return anything if you use void method.
Java:
public static void Mayor(int n,int j){
      if(n>j){
           return n;  // you cannot add return here
       }else{
          return j;  // you cannot add return here
       }
   }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,516
Members
44,991
Latest member
Josephnag

Latest Threads

Top