English 中文(简体)
wers Of River Java
原标题:Towers Of Hanoi Java

这是我正在从事的家务劳动。 我设立了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);
        }
    }
}

我需要帮助的是制定<代码>。wersOfHanoi 由我的<代码>TowersRunner级管理。 我还需要进行反响,显示在比赛结束之前,在我的<代码>TowersOfHanoi类别中,游戏要花多少时间。 基本需要一线,即:System.out.println(“......”+“turns to end>;

我不知道如何正确执行反响。 此外,还可以使班轮班管理<代码>。wersOfHanoi。 <代码>TowersOfHanoi的班级自负起罚款,但我们至少需要2个班级。

Help! 我是 Java的一个创新点,一般而言,我没有太过先进。

问题回答

You don t need the main-Function in the TowersOfHanoi class. Instead, replace your TowersRunner main(String args[]) method with

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 );
}

You can just pass the counter in the function and have it be incremented. For example:

public static void solve(int first_disc, char aTower, char bTower, char cTower, int counter) {
    System.out.println("Currently on turn #" + counter);

    if (first_disc == 1) {
        System.out.println("Disk 1 on tower " + aTower + " moving to tower " + cTower);
    } else {
        solve(first_disc - 1, aTower, cTower, bTower, counter + 1);
        System.out.println("Disk " + first_disc + " on tower " + aTower + " moving to tower " + cTower);
        solve(first_disc - 1, bTower, aTower, cTower, counter + 1);
    }
}

在<代码>索尔韦的第一电话中,你将在1时通过。 如你所知,每当<代码>solve被重新点名时,反射就会增加。

我留待你调整,以恢复反响的最终价值: 如果你确实需要最终价值,你就不需要增加一个参数。 仅凭<>t而不是 撤销 那么,你将尝试并表明如何使你恢复所希望的价值。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签