简体   繁体   English

Java中做时比较

[英]do-while comparison in java

Assuming the role variable has been initialized, I need to know how to do that cycle is repeated while the variable role is different from any of those three strings through a .equals() as is normally done with a single string. 假设角色变量已经初始化,我需要知道如何重复执行该循环,而变量角色通过.equals()不同于这三个字符串中的任何一个, .equals()通常使用单个字符串一样。

do {    
    System.out.println("Input role");
    System.out.println("Administrator / Client / User");
    role=reader.stringReader();
    role=role.toLowerCase();
} while (role!="administrator" || "client" || "user");

First create a method like this: 首先创建这样的方法:

private static boolean isMatch(String input) {
  String[] goodInputs = new String[]{"administrator","client","users"};
  for (int i = 0; i < goodInputs.length; i++) {
    if (input.equals(goodInputs[i])) return true;
  }
  return false;
}

then you code can look like this 那么你的代码可以看起来像这样

do {    
  System.out.println("Input role");
  System.out.println("Administrator / Client / User");
  role=reader.stringReader();
  role=role.toLowerCase();
} while (!isMatch(role));

Assuming you want to check that role!="administrator" && role!="client" && role!="user use: 假设您要检查该role!="administrator" && role!="client" && role!="user使用:

   //role=role.toLowerCase();
} while ( ! role.equalsIgnoreCase("administrator") &&
          ! role.equalsIgnoreCase("client") &&
          ! role.equalsIgnoreCase("user")
          );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM