Java硬编码重复过程

标签 java oop loops hardcode

我正在制作一个基本的硬编码游戏,有 2 个用户互相战斗。我所有的方法都已设置并按预期工作。我现在试图在循环主硬代码后找出一种方法,提供再次战斗并继续游戏的选项,而不是在一场战斗后停止。

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   

最佳答案

尝试将 main 中的所有代码移至新函数中。

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

while(playGame()) {}

如果应该再次玩游戏,则让 playGame() 返回 true,如果应该结束,则返回 false

<小时/>

示例:

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()) {}

}

关于Java硬编码重复过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446493/

相关文章:

java - 知道为什么 Collections.sort() 在下面的程序中不起作用吗?

javascript - 为什么需要在对象中使用 this.property=property ?

language-agnostic - 什么是 : does not rely on . NET 并提供最多 OOP 支持且部署最简单的 Windows 脚本语言?

c - C中的无限循环BUG

java - Android 未捕获的异常日志

java - Wicket @SpringBean 和 Spring @Autowired 通过构造函数注入(inject)

java - 如何捕获 AssertPathsEqual 并将错误打印到控制台?

ruby - 为什么我不能将单例方法附加到另一个对象?

javascript - 循环内循环 lodash

c++ - 遍历不同的对象