河内爪哇塔 [英] Towers Of Hanoi Java

查看:86
本文介绍了河内爪哇塔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在从事的作业.我创建了2个课堂来玩河内塔.第一个基本上是运行实际游戏类的跑步者.

This is a homework that I was working on. I have created 2 classes to play Towers of Hanoi. The first one is the basically a runner to run the actual game class.

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.");

What I need help with is to make the TowersOfHanoi class to run from my TowersRunner class. I also need to implement a counter display how many times it took for the game to run until the game is finished in my TowersOfHanoi class. Basically I need line that is System.out.println("It took" + counter + "turns to finish.");

我不知道如何正确实现计数器.另外,不能使Runner类运行TowersOfHanoi. TowersOfHanoi类本身运行良好,但是对作业的要求是我们至少需要2个类.

I don't know how to implement the counter correctly. Also, can't make the runner class to run the TowersOfHanoi. The TowersOfHanoi class runs fine by itself but the requirment for the homework is we need at least 2 classes min.

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

Help would be much appreciated!!! Please I am a novice in Java and programming in general please don't go too advanced on me. :D

推荐答案

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

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

这篇关于河内爪哇塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆