如何在退出for循环后停止打印null?

时间:2017-07-13 18:55:05

标签: java loops printing null return

所以我正在尝试开发一个简单的基于文本的Java游戏,其中一小部分是玩家库存,而我有点赢(我对java很新)我似乎无法想象如何让null停止在屏幕上打印,把它放到上下文中,我有其他方法来处理杀死敌人等等,如果敌人被杀死然后运行dropChance()方法,如果敌人放弃任何东西然后玩家库存更新敌人掉落的新东西,当玩家遇到敌人时代码的另一部分,玩家有5个选项,攻击,防守等等,其中一个选项是显示他们的库存,在此指出下面的方法被调用并且在打印出玩家库存的所有元素时工作正常,问题如前所述,一旦完成for循环并退出,我们就会移动到返回null并且这也打印出来控制台窗口显然不理想,对此问题的任何帮助都将是g非常感谢,如果我需要更多的代码来获得更全面的理解,那么我很乐意提交。

感谢您提供的任何帮助。

d

    public static List<String> getPlayerInventory()
    {
        for (int i = 0; i < playerInventory.size(); i ++)
        {
            System.out.println(playerInventory.get(i));
        }

        return null;
    }

//战斗循环。                 而(!killedAnEnemy)                 {                     int randomEnemyAttack = random.nextInt(dungeon.getMaxEnemyAttack());                     int randomPlayerAttack = random.nextInt(dungeon.getMaxPlayerAttack());

                System.out.println("\n> What would you like to do: ");
                System.out.println("\n\t * Press 1 to attack.");
                System.out.println("\n\t * Press 2 to defend.");
                System.out.println("\n\t * Press 3 to run away.");
                System.out.println("\n\t * Press 4 to use a health potion.");
                System.out.println("\n\t * Press 5 to display inventory");

                int input = scanner.nextInt();

                switch (input)
                {
                    case 1:
                        System.out.println("\n You attack the " + userEnemyChoice + " and strike it for " + randomPlayerAttack + " damage points!");
                        System.out.println("\n The " + userEnemyChoice + " hits you back for " + randomEnemyAttack);
                        break;
                    case 2:
                        System.out.println("\n You decide that defense is the best option, the " + userEnemyChoice + " attacks you first causing " + randomEnemyAttack + " hit points of damage!");
                        System.out.println("\n You then attack in retaliation and strike the " + userEnemyChoice + " for " + randomPlayerAttack + " hit points of damage");
                        break;
                    case 3:
                        System.out.println("\n You realise you are no match for this enemy and so choose to run, the " + userEnemyChoice + " strikes you one final blow" + "\n for " + randomEnemyAttack + " hit points as you turn your back and are left defenseless!");
                        break;
                    case 4:
                        break;
                    case 5:
                        System.out.println("\n Your inventory is as follows: ");
                        System.out.println("\n\t " + getPlayerInventory());
                        break;
                }

3 个答案:

答案 0 :(得分:0)

在案例5中不要做sysout,只需调用这样的方法

                case 5:
                    System.out.println("\n Your inventory is as follows: \n");
                    getPlayerInventory();
                    break;

答案 1 :(得分:0)

在您的开关盒中,替换

System.out.println("\n\t " + getPlayerInventory());

使用

getPlayerInventory();

这将以您想要的方式工作。它打印“null”的原因是因为你在print方法中进行了调用。它不是必需的,因为getPlayerInventory()中的print方法可以独立工作。

答案 2 :(得分:-1)

如果您总是返回null,那么只需制作方法void即可。如果正在打印null,则听起来就像是在print方法中调用此方法。您不需要这样做,因为库存已经在方法中打印。