jeudi 25 février 2021

Main.java:12: error: missing return statement } ^ [duplicate]

anyone could please explain?

class Main{
    
    static String CountAndSay(int n)
    {
        if (n==1){
        return "1";
        }
        if(n==2)
        {
        return "11";
        }
    }
    public static void main (String[] args) 
    {
        System.out.println(CountAndSay(1));
      
    }
}

Ans:- Main.java:12: error: missing return statement } ^

BUT when I write the return statement after if statement it's working.

class Main{
    
    static String CountAndSay(int n)
    {
        if (n==1){
        return "1";
        }
        if(n==2)
        {
        return "11";
        }
        return "0";
    }
    public static void main (String[] args) 
    {
        System.out.println(CountAndSay(1));
      
    }
}

ans:- 1

Aucun commentaire:

Enregistrer un commentaire