首页 > 解决方案 > 迷宫游戏文本输出

问题描述

所以我有这个迷宫游戏,我试图使用 RNG 来确定当用户撞墙、越界或到达迷宫尽头时打印出哪条消息。我的 TextCard 类中有所有这些消息,但我希望所有文本卡,无论它们用于什么,都有一个边框,所以我创建了另一个名为 buildBorder 的方法,但是当我运行我的代码时,它只输出消息而不是边界,我不知道为什么。

构建器方法

public String buildBorder(String cardType){
        return ("**********************\n" + cardType + "\n" + "**********************");

墙卡法

public String wallCard(){
        Random rnd = new Random();
        number = rnd.nextInt(100) + 1;
        if(number < 20 && number > 1){
            cardText = ("Going that way would lead to a painful face plant");
            return (cardText);
    }
    if(20 < number && number < 40){
        cardText = ("Some are destined for greatness, You are destined for a hard surface.  You cant go this way");
        return (cardText);
    }
    if(40 < number && number < 60){
        cardText = ("Are you lost? or do you just like running into walls?");
        return (cardText);
    }
    if(60 < number && number < 80){
        cardText = ("phasing is not your strong suit.  Find another way, this wall is as hard as your skull, take the hint.");
        return (cardText);
    }
    else{
        cardText = ("You spend longer than you should looking for the door handle, only to realize you ran into a wall.");
        return (cardText);
    }
  }

建设者(部分)

public TextCard(CardType cardType)
    {

       if(cardType == (CardType.WALL)){

           buildBorder(wallCard());

        }

获取卡方法

public String getCard(){
        return this.cardText;
    }

其他类中的方法调用

else if((this.maze.isWall(x, y, "N")) == true){

                     System.out.println(new TextCard(TextCard.CardType.WALL).getCard());

也忘了发布这个,但这些是我的枚举

public enum CardType{
        WALL, OUT, START, END
    }

标签: java

解决方案


当您调用buildBorder(wallCard());它时,它会返回一个字符串。

您不将此字符串分配给任何东西 - 您不打印它,存储它。所以很自然,什么都不会发生。

String temp = buildBorder(wallCard()); // NOW you can do whatever your heart desires


推荐阅读