求救,用java编一个汉诺塔问题的程序

来源:百度知道 编辑:UC知道 时间:2024/06/15 19:42:02
public class Pai {
public static void main(String[]args) {
towers(4,"A","B","C");

}
public static void towers(int n,char frompeg,char topeg,char auxpeg) {
if (n==1) {
System.out.println("move disk 1from peg"+frompeg+"to peg"+topeg);
return;
}
towers(n-1,frompeg,auxpeg,topeg);
System.out.println("move disk"+n+"from peg"+frompeg+"to peg"+topeg);
towers(n-1,auxpeg,topeg,frompeg);

}

}
编译出现了错误,请问应该怎么改啊??

public class Pai {
public static void main(String[]args) {
towers(4,'A','B','C');

}
public static void towers(int n,char frompeg,char topeg,char auxpeg) {
if (n==1) {
System.out.println("move disk1 from peg"+frompeg+"to peg"+topeg);
return;
}
towers(n-1,frompeg,auxpeg,topeg);
System.out.println("move disk"+n+" from peg"+frompeg+"to peg"+topeg);
towers(n-1,auxpeg,topeg,frompeg);

}

}