扫描仪循环中断 + 正则表达式问题
Scanner loop breaks + Regex issue
这个任务是一个单词是否是回文,我用的是java11.
输入带空格的单词后,代码会给出答案,但如果我选择 word = sc.next()
,while 循环会中断并且代码“以退出代码 0 结束”。但是如果我写 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());
}
}
您可以在正则表达式中改进什么,[Y|y][E|e][S|s]
?
改用[Yy][Ee][Ss]
。 []
指定一个字符 class 并且其中的所有字符都表示其中之一,即默认情况下它们带有隐式 OR
。检查 this 以了解更多信息。
如何确定输入是否包含 space?
将" "
传递给函数,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
这个任务是一个单词是否是回文,我用的是java11.
输入带空格的单词后,代码会给出答案,但如果我选择 word = sc.next()
,while 循环会中断并且代码“以退出代码 0 结束”。但是如果我写 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());
}
}
您可以在正则表达式中改进什么,[Y|y][E|e][S|s]
?
改用[Yy][Ee][Ss]
。 []
指定一个字符 class 并且其中的所有字符都表示其中之一,即默认情况下它们带有隐式 OR
。检查 this 以了解更多信息。
如何确定输入是否包含 space?
将" "
传递给函数,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