这是我正在做的作业。我创建了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类运行TowersOfHanoiTowersOfHanoi类本身运行良好,但是对作业的要求是我们至少需要2个类。

帮助将不胜感激!!!请我是Java和程序设计的新手,请不要对我太高级。 :D

最佳答案

您不需要TowersOfHanoi类中的main-Function。
相反,将您的TowersRunner main(String args [])方法替换为

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();
    TowersOfHanoi.solve(num_of_discs, 'A', 'B', 'C');
}

10-06 13:18