Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java循环,直到满足所有方法_Java_Oop_Text Based - Fatal编程技术网

Java循环,直到满足所有方法

Java循环,直到满足所有方法,java,oop,text-based,Java,Oop,Text Based,我正在做一个简单的基于文本的面向对象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"); ch

我正在做一个简单的基于文本的面向对象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循环,但不幸的是,只是循环了相同的方法:( 非常感谢您的帮助:)

问题变量从未分配新值,因此在退出一个“世界”后,您必须使用新值更新问题。
如果循环是无限的,则需要中断或返回以退出循环

@Yani if循环应为if else梯形图,以获得更好的性能,并且应使用新值重新分配问题变量以进入其他战斗,即asknext应在每个方法中使用,即在chooseDungeonBattle中。如果要结束你的任务,你必须在最后一个循环结束后打破while循环。

你需要保存进度(状态),将其存储在某种容器中,例如std::vector,然后添加或删除玩家所做的事情,使容器范围可供你的所有函数访问,然后在容器中循环其余选项。请添加包装while循环的方法。这听起来像是一个通用的解决方案。请提供一些代码片段以便更好地理解。