java表达式太复杂,减少了conditionl运算符的数量

use*_*745 5 java conditional-operator

我们有一个针对我们的代码运行的程序,以遵守一些编码标准.

该计划说:

表达式不应该太复杂,减少使用的条件运算符的数量,他的表达式Min允许3.

如何减少条件运算符的数量?也许把关键事件放在一个数组中?

public boolean onlyNumbers(KeyEvent evt) {
    char c = evt.getKeyChar();
    boolean returnValue = true;
    if (
        !(
            Character.isDigit(c) 
            || c == KeyEvent.VK_BACK_SPACE
            || c == KeyEvent.VK_DELETE 
            || c == KeyEvent.VK_END 
            || c == KeyEvent.VK_HOME
        )
        || c == KeyEvent.VK_PAGE_UP
        || c == KeyEvent.VK_PAGE_DOWN
        || c == KeyEvent.VK_INSERT
    ) {
        evt.consume();
        returnValue = false;
    }
    return returnValue;
}
Run Code Online (Sandbox Code Playgroud)

Dim*_*ima 5

final String junkChars = new String(new char[] {
    KeyEvent.VK_BACK_SPACE,
    KeyEvent.VK_DELETE,
    KeyEvent.VK_END,
    KeyEvent.VK_HOME
    /* The last three seem unused in the latest version
    KeyEvent.VK_PAGE_UP,
    KeyEvent.VK_PAGE_DOWN,
    KeyEvent.VK_INSERT */
});
if(!Character.isDigit(c) && junkChars.indexOf(c) == -1) {
   evt.consume();
   return false;
}  else {
    return true;
}
Run Code Online (Sandbox Code Playgroud)