Resolving “The method must return a result of type int” in Java

后端 未结 9 654
庸人自扰
庸人自扰 2021-01-21 12:37

I\'m very new to Java. Eclipse is giving me the error

The method must return a result of type int

for the following code:



        
9条回答
  •  无人及你
    2021-01-21 13:21

    Yes the problem is that you can only have one public class per file and this file should have the same name than the class. You can just remove the public in front of the definition of the first class. A better way to do would be to make it a static method of the main class.

    To solve you second problem you can do this:

    public class firstprog {
    
    
        public static int largest(int a,int b,int c) 
        {
            if(a>b)
            { 
                if(a>c)
                    return a;
                else 
                    if(b>c)
                        return b;
                    else 
                        return c;
            }
            else
            {
                if(b>c)
                    return b;
                else 
                    return c;
            }
        }   
    
        public static  void main(String args[]) {
    
            int a=19;
            int b=2;
            int c=1;
    
            System.out.println(largest(a,b,c));  
      }
    }
    

提交回复
热议问题