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

后端 未结 9 657
庸人自扰
庸人自扰 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:14

    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;
                  }
             }
             return 0;
        }   
    
        public static  void main(String args[]) {
    
            int a=7;
            int b=8;
            int c=9;
    
            System.out.println(largest(a,b,c));
    
        }
    }
    

    Note: you have to add return statement, because a is not greater then b, so it it not going inside of if block.. In your larget(...) it is expecting return statement as int.. so you have to add one more return statement. then it work ... Cheers ...!!!

提交回复
热议问题