Java输出汉诺塔实现
时间: 2023-03-19 14:20:55 浏览: 128
Java实现汉诺塔游戏.doc
下面是一个用 Java 语言实现汉诺塔的示例代码:public class Hanoi {public static void main(String[] args) {hanoi(3, 'A', 'B', 'C');}public static void hanoi(int n, char from, char inter, char to) {if (n == 1) {System.out.println("从 " + from + " 移到 " + to);} else {hanoi(n - 1, from, to, inter);System.out.println("从 " + from + " 移到 " + to);hanoi(n - 1, inter, from, to);}} }
阅读全文