0

我正在开发一个游戏,我使用一个类,该类允许用户输入一个选项作为字符串来执行 3 个选项之一:“攻击”、“防御”或“逃跑”。这可以通过“异或”(XOR)在逻辑上处理。我决定尝试使用这个逻辑而不是通常的 Switch 构造来使用 while 循环来看看会发生什么,只需要一点点离散数学。我知道 while 条件会很丑很长,但它确实有效!我喜欢它!我想知道,因为如果有某种方法可以保存 while 循环条件(例如作为变量、方法或其他东西),我想在代码的其他部分重用这个 while 循环,以便我可以把它放在while循环条件,每次不占用8行代码。这是我的while循环。别笑,它有效。和,它避免了try-catch。它实际上运行得非常干净。我在下面发布了我使用的方法。此方法由另一个类调用:

public static void fighterAction(){  
    String selection = null;
    Scanner userChoice = new Scanner(System.in);

//Fighter 1 chooses combat action to perform: this is performed by 
//(XOR) logic:
//(( a  || b ) && !( a && b ) || c ) && !((( a || b ) && !( a && b ) && c ))

    while((( !"attack".equals(selection)  || !"defend".equals(selection) ) 
            && !( !"attack".equals(selection) && !"defend".equals(selection)
            ) || !"flee".equals(selection) ) &&
            !((( !"attack".equals(selection) || !"defend".equals(selection)
            ) && !( !"attack".equals(selection) && 
            !"defend".equals(selection) ) && !"flee".equals(selection) )))
    {    
        System.out.println("Choose action: attack  defend  flee\n\nEnter: ");
        selection = userChoice.next();
        if((( !"attack".equals(selection)  || !"defend".equals(selection) ) 
                && !( !"attack".equals(selection) && 
                !"defend".equals(selection) ) || !"flee".equals(selection) )
                && !((( !"attack".equals(selection) || 
                !"defend".equals(selection) ) && 
                !( !"attack".equals(selection) && 
                !"defend".equals(selection) ) && 
                !"flee".equals(selection) )))
        {
            System.out.println("Invalid Entry!");
        }else{
        System.out.println(selection + " was chosen");
        System.out.println("");
        }
    }        
}

再一次,我问是否有办法采用这个 while 子句(我强调,它非常有效):

(( !"attack".equals(selection) || !"defend".equals(selection) ) && !( !"attack".equals(selection) && !"defend".equals(selection) ) || !"逃离".equals(selection) ) && !((( !"attack".equals(selection) || !"defend".equals(selection) ) && !( !"attack".equals(selection) && !"defend ".equals(selection) ) && !"flee".equals(selection) ))

使其适合 while 循环条件:

 while(FITS HERE){}  

谢谢!

4

3 回答 3

4

Whoa, whoa, whoa. selection can only ever have one value at a time. There are a number of refactorings that could make this code much cleaner (like using an enum, and not embedding magic constants), but you can logically collapse that whole thing into

while(!"attack".equals(selection) && !"defend".equals(selection) && !"flee".equals(selection)) { ... }
于 2013-10-15T23:46:26.223 回答
2

It will "fit" as is, in other words you can use it as is, but it will be hard to read and more importantly, hard to debug. To make it prettier, make it into a method.


Having said this, using Strings for this sort of thing is ugly and dangerous, plus it will limit your program should you decide to change the UI to be a GUI interface, say Swing or Android. I would instead consider creating an enum to encapsulate the user options:

public enum UserOption {
  ATTACK, DEFEND, FLEE
}

A method expecting this enum can only accept one of the three enum constants (or null) thus giving you compile-time type checking and a limitation of the user's options and would also result in prettier more re-usable and extensible code.

Then your console app could have a validate method:

public boolean validateUserOptionsString(String text) {
  for (UserOption option : UserOption.Values() {
    if (text.equalsIgnoreCase(option.toString())) {
      return true;
    }
  }
  return false;
}
于 2013-10-15T23:44:45.493 回答
2

我就是这样写的

String select;
while (true) {
   System.out.println("Enter: attack, defend or flee");
   selection = userChoice.nextLine();
   if (select.equals("attack") || select.equals("defend")||select.equals("flee"))
       break;
   System.out.println("Invalid input: " + select);
}
于 2013-10-15T23:52:04.560 回答