简体   繁体   中英

Java - Multiple accepted inputs"

I was just curious how you would accept multiple inputs in Java, from what I have tried you're unable to use the logical OR operators || .

So if I wish to have multiple accepted inputs accepted for an IF statement how would I do this? if (userinput.equalsIgnoreCase("House" || "Home")) How can I have both House and Home activate this without creating an else statement?

采用

if((userinput.equalsIgnoreCase("House")) || (userinput.equalsIgnoreCase("Home")))

If you need to match against many predefined values, you could create a list of allowed values (in fixed case) beforehand and then simply use List.contains() for asserting the correct input:

final List<String> validValues = Arrays.asList("HOUSE", "HOME", "CASA");

if (validValues.contains(userInput.toUpperCase())) {
  // do something
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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