【问题标题】:Trying to move my object initialisation and method calls out of my Main method试图将我的对象初始化和方法调用移出我的 Main 方法
【发布时间】:2013-06-07 19:38:37
【问题描述】:

我正在尝试将一些代码移出 main 方法并在另一个类中使用它。这样做的原因是我了解到这样做是建议的惯例,而且我对 Java 相对较新,因此希望尝试一下。目前,当我将代码移动到“游戏”类时,我无法解决 eclipse 显示的错误。

任何帮助将不胜感激。

playGame 类中的主要方法-

 public class playGame {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            player playerOne = new player();
            GameHelper gameHelper = new GameHelper(); 
            wordManipulator thisWord = new wordManipulator();
            gameHelper.showDisplay();
            playerOne.setPlayerName();
            gameHelper.showIntro(playerOne.getPlayerName());
            thisWord.showHiddenWord();
            playerOne.setPlayerGuess();

        }

    }

游戏类

public class Game {

//other code for the game logic

}

【问题讨论】:

  • 嘿,我想我会添加更多细节,以防它帮助任何人提供答案,这些是我在将代码移动到游戏类时看到的错误:“令牌上的语法错误”showDisplay ", 此标记后应有标识符" 和 "标记 "布尔" 上的语法错误, @ 预期"
  • 感谢任何对此进行调查的人。我最终解决了它。我愚蠢地试图将代码直接放入“游戏”类中,它应该是方法的一部分,即“游戏”类的 startGame。所有排序:)

标签: methods coding-style main convention


【解决方案1】:

确保将您的代码放在 Game 类的方法中,然后调用该特定方法。

另一个提示:类名使用大写。

【讨论】:

  • 谢谢,我知道这是几年前的事了,但我当时没有发表评论——抱歉,这很有帮助。
猜你喜欢
  • 1970-01-01
  • 2016-09-30
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多