这是我正在做的作业。我创建了 2 个类来玩河内塔。第一个基本上是运行实际游戏类的跑步者。
import java.util.Scanner;
class TowersRunner {
public static void main(String[] args) {
TowersOfHanoi towers = new TowersOfHanoi();
towers.TowersOfHanoi()
}
}
public class TowersOfHanoi {
public static void main(String[] args) {
System.out.println("Please enter the starting " + "number of discs to move:");
Scanner scanner = new Scanner(System.in);
int num_of_discs = scanner.nextInt();
solve(num_of_discs, 'A', 'B', 'C');
}
public static void solve(int first_disc, char aTower, char bTower, char cTower) {
if (first_disc == 1) {
System.out.println("Disk 1 on tower " + aTower + " moving to tower " + cTower);
} else {
solve(first_disc - 1, aTower, cTower, bTower);
System.out.println("Disk " + first_disc + " on tower " + aTower + " moving to tower " + cTower);
solve(first_disc - 1, bTower, aTower, cTower);
}
}
}
我需要帮助的是让TowersOfHanoi
班级从我的TowersRunner
班级开始。TowersOfHanoi
我还需要实现一个计数器,显示游戏在我的课堂上完成之前运行了多少次。基本上我需要的线是System.out.println("It took" + counter + "turns to finish.");
我不知道如何正确实现计数器。此外,不能让 runner 类运行TowersOfHanoi
. 课程本身运行良好,TowersOfHanoi
但家庭作业的要求是我们至少需要 2 节课。
帮助将不胜感激!!!请我是Java和一般编程的新手,请不要对我太先进。:D