java基础语法练习--汉诺塔问题

来源:互联网 发布:开淘宝店铺多少钱 编辑:程序博客网 时间:2024/04/30 03:27
package test;import java.io.BufferedReader;import java.io.InputStreamReader;public class aa {   public static void main(String args[]) throws Exception{//抛出异常         int n;        BufferedReader buf =  new BufferedReader(new InputStreamReader(System.in));       System.out.print("请输入盘数:");        n = Integer.parseInt(buf.readLine());        aa hanoi = new aa();        hanoi.move(n, 'A', 'B', 'C');    }    public void move(int n, char a, char b, char c) {        if (n == 1)            System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);        else {            move(n - 1, a, c, b);           System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);         move(n - 1, b, a, c);        }    }}

0 0