Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
换句话说,我想确保用户输入的是 A、a、L 或 l。到目前为止,我的解决方案是这样,但我没有得到正确的正则表达式。
while(!qtype.hasNext("A| L") || !qtype.hasNext("a| l")){ System.out.print("Please enter 'A' or 'L':"); qtype.next().toUpperCase(); } qal = qtype.next().toUpperCase();
要确定字符串str是"A","a"还是您"L"可以"l"使用
str
"A"
"a"
"L"
"l"
str.matches("[AaLl]")
如果您不想使用正则表达式:
str.equalsIgnoreCase("A") || str.equalsIgnoreCase("L")
尝试这样的事情:
while (! (qal = qtype.next()).matches("[AaLl]")) System.out.print("Please enter 'A' or 'L': ");