6、10、18、32、54下一個數是多少?編程實現輸入任意一個N位置,該數是多少?
public class TestLoop { public static void main(String[] args) { int n; Scanner input = new Scanner(System.in); System.out.println("請輸入任意位置N:"); n = input.nextInt(); System.out.println(loop1(n)); } static int loop1(int n){ if(n==1){ return 6; }else{ return loop1(n-1)+loop(n-1); } } static int loop(int n){ if(n==0){ return 0; } if(n==1){ return 4; } else{ return loop(n-1)+2*n; } } }
如果大家有新的解決辦法或者不同的見解,請留言互助。謝謝!