import java.util.Scanner;
public class test {
static long l=0;
public static void main(String args[]) {
System.out.println("请输入盘子个数:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println("汉诺塔层数为" + n);
System.out.println("移动方案为:" );
hanoi(n, 'a', 'b', 'c');
System.out.println("需要移动次数:"+l);
  
} static void hanoi(int n, char a, char b, char c) {
if (n > 0) {
hanoi(n - 1, a, c, b);
move(a, b);
hanoi(n - 1, c, b, a);
l++;
}
} static void move(char x, char y) {
System.out.println(x + "->" + y + "\t");
}
}

解决方案 »

  1.   

    因为你的hanoi()方法里面用到递归,很明显也是循环
      

  2.   

    代码稍改一下就行了:
    import java.util.Scanner;
    public class FacTest {
    static long l=0;
    public static void main(String args[]) {
    System.out.println("请输入盘子个数:");
    Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    System.out.println("汉诺塔层数为" + n);
    System.out.println("移动方案为:" );
    hanoi(n, 'a', 'b', 'c');
    System.out.println("需要移动次数:"+l);
    }
    static void hanoi(int n, char a, char b, char c) {
    if (n ==1 ) 
    System.out.println("需要移动次数:"+l);
    else
    {
    hanoi(n - 1, a, c, b);
    move(a, b);
    hanoi(n - 1, c, b, a);
    l++;
    }
    }static void move(char x, char y) {
    System.out.println(x + "->" + y + "\t");
    }

      

  3.   

    hanoi这个方法递归了,不管条件满不满足都在执行