English 中文(简体)
2. 与制革的转换/申请
原标题:switch / case request with boolean

如果Login(用户名和密码)至少拥有6个果园,那么我就试图制定一种办法。

为了认识到我建立了这一方法<代码>。loginData (final Stringusername, final String Password)。 在这种方法中,我创立了青少年(用户和通过者),我可以创造4个不同的 b链:

  • user: true pass: true
  • user: false pass: true
  • user: false pass: false
  • user: true pass: false

我现在要逐一提出开关/办案要求,但我并没有认识到这一点。

如果你问我为什么需要开关,我就认为我需要开关,因为我想要为这4个 b干链中的每一个环节做事,那是做的。 我也想在一片性别-java-航道上这样做,如果是: 请帮助!

该方法的编码:

public void checkLoginData(final String username, final String password){

    boolean user, pass;

    if (username.length() < 6){
        user = false;
    }else {
        user = true;
    }

    if (password.length() < 6){
        pass = false;
    }else {
        pass = true;
    }

    boolean[] logindaten = {user, pass};



}

• 预先提供帮助。

Best Respectsafari

最佳回答

您只能转而使用<代码>boolean[]。 为了将ole鱼改成 in子,你可以为2只ole鱼使用比照面罩,例如:

int val = 0;
if (user) val |= 0x1;
if (pass) val |= 0x2;

switch (val) {
case 0: // Both too short
case 1: // User Ok, pass too short
case 2: // User too short, pass ok
case 3: // Both Ok
}
问题回答

如果你真心想“性-java-way”(但取决于你的理解),你可以做这样的事情(Java 7要求):

boolean user, pass;

switch (user + "-" + pass) {
    case "false-false":
        ...
    case "false-true":
        ...
    case "true-false":
        ...
    case "true-true":
        ...
    default:
        throw new RuntimeException(
            "something strange happening here, user: " + user + ",pass: " + pass);
}

但是,我更喜欢用自己的信息进行2次不同的检查,这一信息正在合并提出。 (但无法确定这是否可被视为“性别-java-way”,更像是一种工作方式)

• 保证我能以下列方式解决这一问题:

public class LoginController
{
  private void login( String username, String password )
  {
    LoginState state = determineLoginState( username, password );

    switch ( state )
    {
      case LOGIN_OK:
        //Do Something
        break;
      case USERNAME_FALSE:
        //Do Something
        break;
      case PASSWORD_FALSE:
        //Do Something
        break;
      case BOTH_FALSE:
        //Do Something
        break;
    }

  }

  private LoginState determineLoginState( String username, String password )
  {
    final boolean checkUsername = checkUsername( username );
    final boolean checkPassword = checkPassword( password );

    if ( checkUsername && checkPassword )
      return LoginState.LOGIN_OK;

    if ( !checkUsername && checkPassword )
      return LoginState.USERNAME_FALSE;

    if ( checkUsername && !checkPassword )
      return LoginState.PASSWORD_FALSE;

    if ( !checkUsername && !checkPassword )
      return LoginState.BOTH_FALSE;

    throw new AuthenticationException();
  }

  protected boolean checkUsername( String username )
  {
    return username.length() > 6;
  }

  protected boolean checkPassword( String password )
  {
    return password.length() > 6;
  }

  private enum LoginState
  {
    LOGIN_OK, USERNAME_FALSE, PASSWORD_FALSE, BOTH_FALSE;
  }

  public class AuthenticationException extends RuntimeException
  {

  }
}

基本上没有比这更简单的方法,也不可能以大大低于法典的思路来做到这一点。

if (username.length() < 6){
    if (password.length() < 6){
        // do case 1
    } else {
        // do case 2
    }
} else {
    if (password.length() < 6){
        // do case 3
    } else {
        // do case 4
    }
}

我认为,这是最佳解决办法。

我也想在一片性别-java-航道上这样做,如果情况不错,则不会有iff。

If by "sexy-java-way" you mean "clever" or "obscure", then there are other ways to do it. But they certainly don t make the code easier to read / more maintainable.

顺便提一下,以上仅涉及3条......右边THREE.if/code>.


然而,你(最后)的具体例子:

public void checkLoginData(final String username, final String password){
    boolean user, pass;
    if (username.length() < 6){
        user = false;
    }else {
        user = true;
    }
    if (password.length() < 6){
        pass = false;
    }else {
        pass = true;
    }
    boolean[] logindaten = {user, pass};
    ....
}

can be simplified to the following:

public void checkLoginData(final String username, final String password){
    boolean user = username.length() >= 6;
    boolean pass = password.length() >= 6;
    boolean[] logindaten = {user, pass};
    ....
}

Note that simplification is possible here because the actions (the "cases" in your hypothetical switch) can be refactored into simple boolean assignments AND the tests are actually independent of each other. In general you can t do that ...


......但希望能让它更响亮,给我的布局留下深刻印象;

很严重,如果我是你的 b子,你也写了这样的法典,那么我就想到:UN-。 任何认为你为书写模糊不清和不可维持的法典而cl的 b,都是无益的。

if (user) {
    if (pass) {
        // user = true, pass = true
    } else {
        // user = true, pass = false
    }
} else {
    if (pass) {
        // user = false, pass = true
    } else {
        // user = false, pass = false
    }
}

int case = user ? (pass ? 1 : 2) : (pass ? 3: 4);

switch (case) {  
  case 1:
    System.out.println(" user = true, pass = true ");
    break;
  case 2:
    System.out.println(" user = true, pass = false ");
    break;
  case 3:
    System.out.println(" user = false, pass = true ");
    break;
  case 4:
    System.out.println(" user = false, pass = false ");
    break;
  }
}

你可以做这样的事情,然后每例1 2或3例。

switch((route.isComplete()?1:(route.getAuthentic()?2:(route.hasRoute()?3:0)))) {...}

有了java12,你可以在转盘中使用表述,并提供<条码>Bool类型(

private static boolean checkCharOf_(String userName){

  return userName.length() >= 6;

}

private static boolean checkCharOf_(String passw或d){

  return passw或d.length() >= 6;

}

private static boolean checkCharOf_And_(String userName, String passw或d){

  return userName.length() >= 6 && passw或d.length() >= 6;

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签