提问人:Nkimbiku 提问时间:7/23/2020 最后编辑:Nkimbiku 更新时间:7/23/2020 访问量:80
扫描仪循环中断 + 正则表达式问题
Scanner loop breaks + Regex issue
问:
这个任务是单词是否是回文,我使用 java 11。
输入带空格的单词后,代码给了我一个答案,但是当循环中断并且代码“以退出代码 0 完成”时,如果我选择.但是如果我写 - 它执行得很好并再次开始循环。word = sc.next()
word = sc.nextLine()
我不明白为什么,因为我无法调试这部分代码,它只是跳过了。我不明白为什么会这样。那么有人可以向我解释我做错了什么吗?
也许有一种方法可以避免这个空间?
P.S. 我是编程新手。谢谢大家。
try {
Pattern pattern = Pattern.compile("[Y|y][E|e][S|s]");
while (true) {
String word = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the word without spaces");
if (sc.hasNext("(?>\\p{Alpha})+")) {
word = sc.next();
System.out.println(word);
System.out.println("The word is correct");
String text2;
StringBuilder sb = new StringBuilder();
text2 = sb.append(word).reverse().toString();
if (word.equalsIgnoreCase(text2)) {
System.out.println("Yes, it's a palindrome." + " " + "Want to try another one?");
System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
if (sc.hasNext(pattern)) {
}
else {
break;
}
}
else {
System.out.println("No, it's not a palindrome." + " " + "Want to try another one?");
System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
if (sc.hasNext(pattern)) {
}
else {
break;
}
}
}
else {
word = sc.next();
System.out.println("It's not a word");
System.out.println("Want to try another one?");
System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
if (sc.hasNext(pattern)) {
}
else {
break;
}
}
}
}
catch (NoSuchElementException ex) {
System.out.println(ex.getMessage());
}
}
答:
0赞
Arvind Kumar Avinash
7/23/2020
#1
您可以在正则表达式中改进什么?[Y|y][E|e][S|s]
请改用。指定一个字符类,其中的所有字符都表示其中一个,即默认情况下它们带有隐式 .检查此内容以了解更多信息。[Yy][Ee][Ss]
[]
OR
如何确定输入是否包含空格?
传递给函数 String#contains
以确定它。" "
演示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Enter a word without spaces: ");
String word = sc.nextLine();
if (word.contains(" ")) {
System.out.println("It's not a word");
} else {
System.out.println("The word is correct");
StringBuilder reverse = new StringBuilder(word).reverse();
System.out.println(word.equalsIgnoreCase(reverse.toString()) ? "Palindrome" : "Not a palindrome");
}
System.out.println("Want to try another one?");
System.out.print("Enter yes if you want to continue or any other symbol otherwise: ");
if (!sc.nextLine().matches("[Yy][Ee][Ss]")) {
break;
}
}
}
}
示例运行:
Enter a word without spaces: Hello World
It's not a word
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: yes
Enter a word without spaces: HELLO
The word is correct
Not a palindrome
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: YES
Enter a word without spaces: Malayalam
The word is correct
Palindrome
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: Yes
Enter a word without spaces: papa
The word is correct
Not a palindrome
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: n
评论
0赞
Nkimbiku
7/23/2020
不,我不想只想要输出,我想要一个循环(当我问人们是否想继续时)。在意想不到的“带空格的单词”之后,代码给了我解决方案,然后问我是否要继续,循环中断了。所以我试图理解为什么。当它发生时,我在上面提到过。
0赞
Arvind Kumar Avinash
7/23/2020
@Nkimbiku - 我已经根据您的澄清更新了答案。我希望它有所帮助。
0赞
Nkimbiku
7/23/2020
它可以工作,但是当我在“String word = sc.next();”上更改部分“String word = sc.nextLine();”时,您的代码也有同样的问题。因此,例如,我想在空格和循环中断之前测试一个单词/字符。
0赞
Arvind Kumar Avinash
7/23/2020
@Nkimbiku - 你永远不应该在循环中使用;请改用。检查此内容以了解更多信息。如果你仍然想使用 ,你需要使用额外的字符来使用换行符,这会使你的代码变得混乱。如有任何进一步的疑问/问题,请随时发表评论。Scanner#next
Sacnner#nextLine
Scanner#next
Scanner#next
0赞
Nkimbiku
7/24/2020
谢谢你:)我想,我明白了。
评论
sc.hasNext(pattern)