汉诺塔已经实现,现在我想在每个盘子移动前输出,"第N步"这样的句子,不过居然不知道加在哪里?指教下用java写的,别介意哈
/**
 * 
 *//**
 * @author Administrator
 * 
 */
public class hannio { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
int m; // 汉诺塔盘子数 m = 5;
System.out.println("移动" + m + "个盘子的汉诺塔的步骤:"); hanno(m, 'A', 'B', 'C');
} /**
 * 移动函数
 */
private static void move(char x, char y) {
System.out.println("从" + x + "盘移动一个盘子到" + y + "盘");
} /**
 * 汉诺递归函数
 */
private static void hanno(int n, char one, char two, char three) {
if (n == 1) {
move(one, three);
} else {
hanno(n - 1, one, three, two);
move(one, three);
hanno(n - 1, two, one, three);
}
}}

解决方案 »

  1.   

        /**
         * 移动函数
         */    
        private static void move(char x, char y) {
            System.out.println("第" + (++N) + "步:");
            System.out.println("从" + x + "盘移动一个盘子到" + y + "盘");
        }
      

  2.   

    可以了,确实如LS所说,多谢/**
     * 
     *//**
     * @author Administrator
     * 
     */
    public class hannio { /**
     * @param args
     */
    static int count=1;
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int m; // 汉诺塔盘子数 m = 5;
    System.out.println("移动" + m + "个盘子的汉诺塔的步骤:"); hanno(m, 'A', 'B', 'C');
    } /**
     * 移动函数
     */
    private static void move( char x, char y) {
    System.out.print("第" + (count++) + "步:");
    System.out.println("从" + x + "盘移动一个盘子到" + y + "盘");
    } /**
     * 汉诺递归函数
     */
    private static void hanno(int n, char one, char two, char three) {
    if (n == 1) {
    move( one, three);
    } else {
    hanno(n - 1, one, three, two);
    move(one, three);
    hanno(n - 1, two, one, three);
    }
    }}
      

  3.   

    /**
     * 
     *//**
     * @author Administrator
     * 
     */
    public class hannio {    /**
         * @param args
         */
        int step = 1; // 第Step步
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int m; // 汉诺塔盘子数        m = 5;
            System.out.println("移动" + m + "个盘子的汉诺塔的步骤:");        hanno(m, 'A', 'B', 'C');
        }    /**
         * 移动函数
         */    
        private static void move(char x, char y) {
            System.out.println("第"+(step++)+"从" + x + "盘移动一个盘子到" + y + "盘");
        }    /**
         * 汉诺递归函数
         */
        private static void hanno(int n, char one, char two, char three) {
            if (n == 1) {
                move(one, three);
            } else {
                hanno(n - 1, one, three, two);
                move(one, three);
                hanno(n - 1, two, one, three);
            }
        }}
      

  4.   

    System.out.println("第"+(step++)+"步 从" + x + "盘移动一个盘子到" + y + "盘");
      

  5.   

    public class hannio {
      static int N = 0;    public static void Main() {
            int m = 5; // 汉诺塔盘子数
            System.out.println("移动" + m + "个盘子的汉诺塔的步骤:");
            hanno(m, 'A', 'B', 'C');
        }    private static void move(char x, char y) {
            System.out.println("第" + (++N) + "步: 从" + x + "盘移动一个盘子到" + y + "盘");
        }    private static void hanno(int n, char one, char two, char three) {
            if (n == 1) {
                move(one, three);
            } else {
                hanno(n - 1, one, three, two);
                move(one, three);
                hanno(n - 1, two, one, three);
            }
        }
    }