publicvoid move(int n ,char one ,char two, char three){ if(n==1) System.out.println("第"+n+"只盘子由"+one+"--"+three); else{ move(n-1, one , three , two);//将第n-1只盘子从one通过three移动到two; System.out.println("第"+n+"只盘子由"+one+"--"+three); move(n-1, two , one , three););//将第n-1只盘子从two通过one移动到three; } }
然后我们要做的就是通过一个主函数来调用这个方法就可以了。
publicclass HanN { publicstaticvoid main(String [] args) { HanN hn =new HanN(); hn.move(3,'A','B','C'); } }