第1步:代码逻辑
package com.lian.pojo;
public class Hnt {
public static void main(String[] args) {
Tower tower = new Tower();
tower.move(2,'A','B','C');
}
}
class Tower{
public void move(int num,char x,char y,char z){
System.out.println("lalalalalala");
if (num == 1){
System.out.println(x +"->" + z);
}else {
move(num-1,x,z,y);
System.out.println("bbbbbbbbbb");
System.out.println(x +"->" + z);
System.out.println("aaaaaaaaaaa");
move(num-1,y,x,z);
}
}
}
第2步:结果展示
lalalalalala
lalalalalala
A->B
bbbbbbbbbb
A->C
aaaaaaaaaaa
lalalalalala
B->C