0

私は、2 人のユーザーが互いに戦う基本的なハードコードされたゲームを作成しています。すべてのメソッドが設定され、期待どおりに機能します。私は現在、メインのハードコードをループした後、1 回の戦闘後に停止するのではなく、再度戦闘してゲームを続行するオプションを提供する方法を見つけようとしています。

public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.print("What is your name: ");
        String myName = in.nextLine();

        Fighter a = new Warrior();
        Fighter b = new Dragon();
        a.pickOpponent(b);

        a.setName(myName);
        b.setName("Onyxia");

        System.out.print(getWelcome());     
        while(in.hasNextLine()) 
        {
            switch(in.nextLine()) 
            {
                case "no":
                    System.out.println("Wow, you are not even gonna try, you have lost!");
                    break;
                case "yes":
                    System.out.println("Let the fight begin! ");
                    while(a.isAlive() && b.isAlive()) 
                    {
                        System.out.println("Do you want to punch, kick, or headbutt the other fighter? ");
                        switch(in.nextLine()) 
                        {
                            case "punch":
                                System.out.println(a.getPunch(b));
                                System.out.println(b.getOpponentAttack(a));
                                break;
                            case "kick":
                                System.out.println(a.getKick(b));
                                System.out.println(b.getOpponentAttack(a));
                                break;
                            case "headbutt":
                                System.out.println(a.getHeadbutt(b));
                                System.out.println(b.getOpponentAttack(a));
                                break;
                            default :
                                System.out.println(invalidInput());
                                break;
                        }
                    }
                default:
                    System.out.println(a.getWinner(b));
                    break;  
            }//end of first switch statement
        }//end of first while loop
    }//end of main   
4

2 に答える 2

0

メインを閉じる直前に、while(true)上に置いて閉じます。System.out.print(getWelcome());次に、ユーザーに次のような質問をすることができます

System.out.println("Dare yie try again?");
String answer = in.nextLine();
if(answer.equals("yes"))
    break; //which would break out of the while loop.
于 2015-09-07T22:03:04.793 に答える