【问题标题】:Scanner loop breaks + Regex issue扫描仪循环中断 + 正则表达式问题
【发布时间】:2020-11-13 07:14:00
【问题描述】:

这个任务是一个单词是否是回文,我用的是java 11。

在输入带有空格的单词后,代码会给我一个答案,但是如果我选择 word = sc.next(),则循环中断并且代码“以退出代码 0 结束”。但是如果我写word = sc.nextLine() - 它执行得很好并再次开始循环。

我不明白为什么,因为我无法调试这部分代码,它只是被跳过了。我不明白为什么会这样。那么有人可以向我解释我做错了什么吗?

也许有办法避免这些空格?

附:我是编程新手。谢谢大家。

    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());
    }

}

【问题讨论】:

  • 嗨!您在这里放置了太多代码。您应该缩小您遇到的具体问题的范围。只要看看你的第一句话,sc.nextLine(),就会看到换行符。所以这可能是其中的一部分。另外关于您的 reg ex 解决方案。我不认为这是解决它的最简单方法。你为什么用 reg ex 做呢?
  • 你为什么要做 -> (sc.hasNext(pattern)) 而不仅仅是 sc.next?我也不明白。
  • 我需要“模式”作为决定人是否想尝试生词的结果。那么正则表达式呢 - 我可以创建新的“如果”来测试单词是否带有空格并决定如何处理它,继续与否。也许会有所帮助。
  • @GamingFelix 是对的。你不需要sc.hasNext(pattern) 和这么复杂的代码。如果您对编程很认真,则应尽一切努力使您的代码保持简单。

标签: java regex java.util.scanner


【解决方案1】:

你可以在你的正则表达式中改进什么,[Y|y][E|e][S|s]

请改用[Yy][Ee][Ss][] 指定一个字符类,其中的所有字符都表示其中一个字符,即默认情况下它们带有隐式OR。查看this 了解更多信息。

如何判断输入是否包含空格?

" " 传递给函数,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

【讨论】:

  • 不,我不想只输出,我想要一个循环(当我问他/她是否想继续时)。在意外的“带空格的单词”之后,代码为我提供了解决方案,同时询问我是否要继续并且循环中断。所以我试图理解为什么。当它发生时,我在上面提到过。
  • @Nkimbiku - 我已经根据您的说明更新了答案。我希望它有所帮助。
  • 它可以工作,但是当我更改部分“String word = sc.nextLine();”时,您的代码也有同样的问题关于“字符串字 = sc.next();” .因此,例如,我想在空格和循环中断之前测试一个单词/字符。
  • @Nkimbiku - 你不应该在循环中使用Scanner#next;请改用Sacnner#nextLine。检查this 了解更多信息。如果您仍想使用Scanner#next,则需要使用额外的Scanner#next 来消耗换行符,这会使您的代码混乱。如果有任何进一步的疑问/问题,请随时发表评论。
  • 谢谢 :) 我想,我明白了。
猜你喜欢
  • 2014-10-31
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
相关资源
最近更新 更多