代码之家  ›  专栏  ›  技术社区  ›  Owen D.

如何在基于文本的Java游戏中使用while循环?

  •  0
  • Owen D.  · 技术社区  · 7 年前

    我想要的东西:

    有3个选项的主菜单

    菜单(营地)

    1. 任务(激活任务循环)

    探索 与随机怪物战斗并获得金牌/分数的游戏循环 “怪物出现”

    1. 返回主回路

    2. 使用项目 选择项目 使用项目 应用的项目效果

    3. 跑 返回主菜单(激活主菜单)

    允许你在“装备”上花费金币以增加生命值和伤害值

    “我是铁匠之类的”

    1. 升级武器(增加伤害)

    当玩家死亡或选择退出游戏时,游戏结束

    下面只是逻辑的一个原型

    它工作不正常,结果只是做了一个 无限循环。我希望你们中的一个能 方向我们将不胜感激!

    还有关于营地、地牢、城镇的布尔人的评论吗? 我不知道我是否真的需要这些,可能只是

    import java.util.Scanner;
    
    
    public class logic
    {
        public static void main(String[] args)
        {
    
        boolean running = true;
        Scanner in = new Scanner(System.in);
        System.out.println("This is a test");
    
        while(running)
        {
            boolean camp = true;
            boolean dungeon = false;
            boolean town = false;
            String input = in.nextLine();
            while(camp)
            {
                System.out.println("what u do?");
                System.out.println("1. go dungeon");
                System.out.println("2. go town");
                System.out.println("3. quit");
                if (input.equals("1"))
                {
                    dungeon = true;
                    while(dungeon)
                    {
                        System.out.println("you are in the dungeon");
                        dungeon = false;
                        break;
    
                    }
    
    
    
                }
                else if (input.equals("2"))
                {
                    dungeon = false;
                    town = true;
                    while(town)
                    {
                        System.out.println("you are in the town");
                        town = false;
                        break;
    
                    }
    
    
                }
                else if (input.equals("3"))
                {
                    break;
    
    
                }
                else
                {
                    System.out.println("invalid command!");
    
    
                }
    
    
            }
    
    
        }
    
        }
    
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Programmerion    7 年前

    就我个人而言,我不会使用那么多while循环,而是将每个部分的逻辑放入其自己的函数中,然后使用if-else-if/switch语句来决定应该调用哪个函数,并在while循环中执行此操作,遗憾的是,我无法对此进行测试,因为我没有;我有一个Java环境,我已经有一段时间没有使用Java了。

        2
  •  0
  •   Owen D.    7 年前

    这是我提出的新代码。我能够想出如何将所有不同的领域分解成各自的方法。这将使构建游戏世界变得更加容易:D

    import java.util.Scanner;
    
    
    public class LogicTest
    {
        Scanner in = new Scanner(System.in);
    
        public static void main(String[] args)
        {
            LogicTest game;
            game = new LogicTest(); // a new instance of the public class folder has to be created in order to use all the other methods
    
            game.run();
    
    
        }
    
        public void run() // this method is used to run the game from the main method
            {
                camp();
            }
    
        public void quest() 
        {
            System.out.println("you go on quest");
            camp();
        }
        public void town()
        {
            System.out.println("you go to town");
            camp();
        }
        public void camp() // this method acts as the game hub where the player can choose what they want to do next
        {
            System.out.println("you are in your camp");
            System.out.println("----------------------");
            System.out.println("what do you want to do?");
            System.out.println("1. go quest");
            System.out.println("2. go town");
            System.out.println("3. quit");
    
    
            String input = in.nextLine(); 
            // each one of these options just calls its respective method
                                        // the methods are what have the menus and everything for each location
            if (input.equals("1"))
            {
                quest();
            }
            else if (input.equals("2"))
            {
                town();
            }
            else if(input.equals("3")) // by leaving this empty the program has nothing else to read with this option and so it closes
            {
    
            }
            else 
            {
                camp();
            }
        }
    
    
    
    }