有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩歲。最后問第一個人,他說是10歲。請問第五個人多大?


package C;

public class Wugeren {

    static int age(int n)
        {
            int c;
            if( n==1 ) c=10;
            else   c=age(n-1)+2;
            return c;
        }
    
    public static void main(String[] args) {
//    int a1=10;
//    int a2=a1+2;
//    int a3=a2+2;
//    int a4=a3+2;
//    int a5=a4+2;
//    System.out.println("第五個人的年齡為 "+a5+" 歲");
        
         
        
         System.out.println("第五個人的年齡:"+Wugeren.age(5));

    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM