设计思想:利用递归,把n-1个圆盘先移动b,把最后一个圆盘移到c上,以这样的方法移动,然后以这n-1个盘重新开始,知道所有盘都移到c上

流程图:

代码:

import java.util.Scanner;


public class Hanoi {

public static void main(String[] args)
{
Hanoi a =new Hanoi();
System.out.println("输入盘子数量:");
Scanner input = new Scanner(System.in);
int n =input.nextInt();
a.hanoi(n, 'A', 'B', 'C');
}
void hanoi(int n,char one,char two,char three)
{

if(n==1)
move(one,three);

else
{
hanoi(n-1,one,three, two);
move(one,three);
hanoi(n-1,two,one, three);
}
}
void move(char x,char y)
{
System.out.println(x+"移到"+y);
}
}

结果: