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

1

尝试将所有代码main移入一个新函数中。

您可以将该函数放入main方法中的 while 循环中,如下所示:

while(playGame()) {}

如果应该再次播放,并且应该结束,则playGame()返回。truefalse


样本:

public static boolean playGame() {

        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 playGame  

public static void main(String[] args) {

    while(playGame()) {}

}
于 2015-09-07T21:59:51.220 回答
0

我会在你关闭 main 之前把它放在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 回答