【问题标题】:Java hardcoded repeating processJava硬编码重复过程
【发布时间】:2015-09-07 21:54:35
【问题描述】:

我正在制作一个基本的硬编码游戏,它有 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   

【问题讨论】:

    标签: java oop loops hardcode


    【解决方案1】:

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

    【讨论】:

    • 所以它应该是一个布尔值而不是一个整数?用于 playGame 方法。
    • 你确定int作为布尔值在java中像这样工作吗?我不知道(虽然我在 C++ 中看到过)
    • @BenKnoble 我刚刚检查过,你是对的。似乎它们不是那样工作的——用多种语言编程的危险...... :)
    • 输入 system.out.println 是一个非常糟糕的习惯;它永远不会在 C# 中工作
    【解决方案2】:

    我会将while(true) 放在System.out.print(getWelcome()); 上方,并在您关闭main 之前将其关闭。然后你可以问用户类似

    System.out.println("Dare yie try again?");
    String answer = in.nextLine();
    if(answer.equals("yes"))
        break; //which would break out of the while loop.
    

    【讨论】:

      猜你喜欢
      • 2012-10-17
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 2016-05-16
      • 2018-10-30
      • 1970-01-01
      相关资源
      最近更新 更多