首页 > 解决方案 > Java循环直到满足所有方法

问题描述

我正在做一个简单的基于文本的 OOP java 游戏。我现在有点卡住了,希望能得到帮助

这是代码

    while(true){

        if(question==1){
            String input1 = inputString("You have entered the Dungeon, here you find a " + t.getDesc() + " would you like to fight it? Type Yes/No");
        chooseDungeonbattle(input1);    


        }
          if(question == 2){

       String input2 = inputString("You have entered the Underworld, it is dark and creepy, but you are not scared...\n SNAP! There is a " + d.getDesc() + " do you wish to fight it?");
      chooseUnderworldBattle(input2);



        }else if(question == 3){
       askUser("You have entered the Dark Forest and you met the Evil Elf, do you wish to fight it? ");
       String input3 = inputString("You have entered the Underworld, here you find a " + t.getDesc() + " would you like to fight it? Type Yes/No");
       //chooseAbattle3(input3);


        }
   //If user choose to go to Dungeon



          public static void chooseDungeonbattle(String questionInput){
        if(questionInput.equals("Yes")||questionInput.equals("yes")){
            Battle b = new Battle();
            Troll enemy = new Troll(10);
            MainPlayer main = new MainPlayer( 100, 100,150,0 );
            BadPlayer p = new BadPlayer("",0,0,0);

            int goldReceivedDungeon;



          b.getOutcome(enemy, main);

         if(b.getOutcome(enemy, main).equals("You win")){
        System.out.println("You win and receive " + enemy.awardCarried());
        goldReceivedDungeon=main.getGold()+ enemy.awardCarried();
        System.out.println("You currently have " + enemy.awardCarried());

        }else{
        System.out.println("Sorry you lost");
        }

       }



        }


        public static void chooseUnderworldBattle(String questionInput){
        if(questionInput.equals("Yes")||questionInput.equals("yes")){
            Battle b = new Battle();
            Demon enemy = new Demon(20);
            MainPlayer main = new MainPlayer( 100, 100,150,0 );
            String asknext;
            int goldReceivedDungeon;



          b.getOutcome(enemy, main);

         if(b.getOutcome(enemy, main).equals("You win")){
        System.out.println("You win and receive " + enemy.awardCarried());
        goldReceivedDungeon=main.getGold()+ enemy.awardCarried();
        asknext = inputString("Would you like to move to you next quest? Press 1 for the Dungeon or 3 for The Dark forest");


        }else{
        System.out.println("Sorry you lost");
        }

       }



        }
public static void chooseForestBattle(String questionInput){
    if(questionInput.equals("Yes")||questionInput.equals("yes")){
        Battle b = new Battle();
        Elf enemy = new Elf(20);
        MainPlayer main = new MainPlayer( 100, 100,150,0 );

        int goldReceivedDungeon;



      b.getOutcome(enemy, main);

     if(b.getOutcome(enemy, main).equals("You win")){
    System.out.println("You win and receive " + enemy.awardCarried());
    goldReceivedDungeon=main.getGold()+ enemy.awardCarried();
    asknext = inputString("Would you like to move to you next quest? Press 1 for the Dungeon or 3 for The Dark forest");


    }else{
    System.out.println("Sorry you lost");
    }

   }





    }

我要做的是让主角输入他们想先去的地方(地牢,冥界或黑暗森林),如果他们选择先去冥界并在那里获胜,则输入他们想去的地方之后那 - 地牢或黑暗森林。一直如此,直到任务完成,他们从每个任务中收集奖励。我正在尝试使用while循环,但不幸的是循环使用相同的方法:(非常感谢任何帮助:)

标签: javaooptext-based

解决方案


question变量永远不会被分配一个新值,因此在退出一个“世界”后,您必须使用新值更新question。你的循环是无限的,你需要一个 break 或 return 来退出循环。


推荐阅读