Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何编写代码来指示用户输入的文本中的第一个位置违反了回文属性?_Java_Stack_Queue_Palindrome - Fatal编程技术网

Java 如何编写代码来指示用户输入的文本中的第一个位置违反了回文属性?

Java 如何编写代码来指示用户输入的文本中的第一个位置违反了回文属性?,java,stack,queue,palindrome,Java,Stack,Queue,Palindrome,说我会输入“在你看到厄尔巴岛之前你有能力”。这看起来像是回文,直到您看到“were”中的第一个“e”,因此此文本的输出为: 这不是回文 在以下位置检测到不匹配:Ablewe 下面是司机 public static void main(String[] args) { Scanner input = new Scanner(System.in); String line; do { // prompt user for

说我会输入“在你看到厄尔巴岛之前你有能力”。这看起来像是回文,直到您看到“were”中的第一个“e”,因此此文本的输出为: 这不是回文 在以下位置检测到不匹配:Ablewe

下面是司机

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String line;

        do {

            // prompt user for input String and store it in line
            System.out.print("Enter an expression (or hit Enter to exit) : ");
            line = input.nextLine();

            // convert input String to upper case
            line = line.toUpperCase();

            // if user hits Enter or simply types in spaces and hits enter, break out of loop
            if (line.trim().isEmpty()) {
                break;
            }

            // call isPalindrom
            // if it returns true, display one message, else display another
            if (Palindrome.isPalindrome(line)) {
                System.out.println("Your expression is a palindrome.");
            } else {
                System.out.println("Your expression is not a palindrome.");
            }

        } while (line.length() != 0);

        System.out.println("You didn't enter an expression.  Exiting application ...");
    }
}
下面是具有isAlindrome方法的类

publicstaticbooleanispalindrome(字符串输入){
//创建队列和字符堆栈
//存储输入字符串的步骤
队列q=新的LinkedList();
堆栈s=新堆栈();
//临时存储单个字符
//在输入字符串中,然后将其推送到
//堆栈并添加到队列中
字符字母;
//跟踪角色之间的差异
//在堆栈和队列中
整数不匹配=0;
//循环输入字符串
对于(int i=0;i
我需要输出来显示不匹配发生的位置

  • 通过将所有非字母替换为空字符串,即
    input.replaceAll(“[^\\p{L}]”,创建字符串(
    allLetters
    ,在下面给出的代码中),其中
    ^\\p{L}
    指定一个字符串
  • 使用变量(
    索引
    ,在下面给出的代码中)跟踪已处理字母的位置。当检测到不匹配时,打印
    所有字母.substring(0,索引+1)
    并中断循环
  • 代码:

    int index = 0;
    String allLetters = input.replaceAll("[^\\p{L}]", "");
    // while the Queue isn't empty
    while (!q.isEmpty()) {
        // remove a Character from the Queue
        // pop a Character from the Stack
        // if they're not equal, increment mismatches
        if (!q.remove().equals(s.pop())) {
            // Stack will produce the input String backwards
            // Queue will produce the input String forwards
            mismatches++;
            System.out.println("Mismatch detected at: " + allLetters.substring(0, index + 1));
            break;
        }
        index++;
    }
    
    Enter an expression (or hit Enter to exit) : Able were you ere you saw Elba
    Mismatch detected at: ABLEWE
    Your expression is not a palindrome.
    Enter an expression (or hit Enter to exit) : 
    
    Enter an expression (or hit Enter to exit) : Able were you ere you saw Elba
    Mismatch detected at: Ablewe
    Your expression is not a palindrome.
    Enter an expression (or hit Enter to exit) : 
    
    运行示例:

    int index = 0;
    String allLetters = input.replaceAll("[^\\p{L}]", "");
    // while the Queue isn't empty
    while (!q.isEmpty()) {
        // remove a Character from the Queue
        // pop a Character from the Stack
        // if they're not equal, increment mismatches
        if (!q.remove().equals(s.pop())) {
            // Stack will produce the input String backwards
            // Queue will produce the input String forwards
            mismatches++;
            System.out.println("Mismatch detected at: " + allLetters.substring(0, index + 1));
            break;
        }
        index++;
    }
    
    Enter an expression (or hit Enter to exit) : Able were you ere you saw Elba
    Mismatch detected at: ABLEWE
    Your expression is not a palindrome.
    Enter an expression (or hit Enter to exit) : 
    
    Enter an expression (or hit Enter to exit) : Able were you ere you saw Elba
    Mismatch detected at: Ablewe
    Your expression is not a palindrome.
    Enter an expression (or hit Enter to exit) : 
    
    注意:当前,您正在将转换为大写的用户输入传递给函数,
    isAlindrome
    ,因此输出字符串(
    ABLEWE
    在上面显示的示例运行中)是大写的。更改案例的更好位置是
    isPalindrome
    内部,如下所示:

    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    import java.util.Stack;
    
    public class Palindrome {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            String line;
    
            do {
    
                // prompt user for input String and store it in line
                System.out.print("Enter an expression (or hit Enter to exit) : ");
                line = input.nextLine();
    
                // if user hits Enter or simply types in spaces and hits enter, break out of
                // loop
                if (line.trim().isEmpty()) {
                    break;
                }
    
                // call isPalindrom
                // if it returns true, display one message, else display another
                if (Palindrome.isPalindrome(line)) {
                    System.out.println("Your expression is a palindrome.");
                } else {
                    System.out.println("Your expression is not a palindrome.");
                }
    
            } while (line.length() != 0);
    
            System.out.println("You didn't enter an expression.  Exiting application ...");
        }
    
        public static boolean isPalindrome(String input) {
            // Create a string by replacing all non-letters by empty string
            String allLetters = input.replaceAll("[^\\p{L}]", "");
    
            // convert input String to upper case
            input = input.toUpperCase();
    
            // create Queue and Stack of Characters
            // to store the input String
            Queue<Character> q = new LinkedList<>();
            Stack<Character> s = new Stack();
    
            // temporarily store the individual Characters
            // in input String before they're pushed onto
            // Stack and added to Queue
            Character letter;
    
            // keep track of differences between Characters
            // in Stack and Queue
            int mismatches = 0;
    
            // loop through input String
            for (int i = 0; i < input.length(); i++) {
                // get current Character
                letter = input.charAt(i);
    
                // if the current Character is an alphabetic character
                if (Character.isLetter(letter)) {
                    // push it onto the Stack
                    s.push(letter);
                    // add it to the Queue
                    q.add(letter);
                }
            }
    
            int index = 0;
            // while the Queue isn't empty
            while (!q.isEmpty()) {
                // remove a Character from the Queue
                // pop a Character from the Stack
                // if they're not equal, increment mismatches
                if (!q.remove().equals(s.pop())) {
                    // Stack will produce the input String backwards
                    // Queue will produce the input String forwards
                    mismatches++;
                    System.out.println("Mismatch detected at: " + allLetters.substring(0, index + 1));
                    break;
                }
                index++;
            }
    
            // return true if there are no mismatches, else return false
            return (mismatches == 0);
        }
    }
    
  • 通过将所有非字母替换为空字符串,即
    input.replaceAll(“[^\\p{L}]”,创建字符串(
    allLetters
    ,在下面给出的代码中),其中
    ^\\p{L}
    指定一个字符串
  • 使用变量(
    索引
    ,在下面给出的代码中)跟踪已处理字母的位置。当检测到不匹配时,打印
    所有字母.substring(0,索引+1)
    并中断循环
  • 代码:

    int index = 0;
    String allLetters = input.replaceAll("[^\\p{L}]", "");
    // while the Queue isn't empty
    while (!q.isEmpty()) {
        // remove a Character from the Queue
        // pop a Character from the Stack
        // if they're not equal, increment mismatches
        if (!q.remove().equals(s.pop())) {
            // Stack will produce the input String backwards
            // Queue will produce the input String forwards
            mismatches++;
            System.out.println("Mismatch detected at: " + allLetters.substring(0, index + 1));
            break;
        }
        index++;
    }
    
    Enter an expression (or hit Enter to exit) : Able were you ere you saw Elba
    Mismatch detected at: ABLEWE
    Your expression is not a palindrome.
    Enter an expression (or hit Enter to exit) : 
    
    Enter an expression (or hit Enter to exit) : Able were you ere you saw Elba
    Mismatch detected at: Ablewe
    Your expression is not a palindrome.
    Enter an expression (or hit Enter to exit) : 
    
    运行示例:

    int index = 0;
    String allLetters = input.replaceAll("[^\\p{L}]", "");
    // while the Queue isn't empty
    while (!q.isEmpty()) {
        // remove a Character from the Queue
        // pop a Character from the Stack
        // if they're not equal, increment mismatches
        if (!q.remove().equals(s.pop())) {
            // Stack will produce the input String backwards
            // Queue will produce the input String forwards
            mismatches++;
            System.out.println("Mismatch detected at: " + allLetters.substring(0, index + 1));
            break;
        }
        index++;
    }
    
    Enter an expression (or hit Enter to exit) : Able were you ere you saw Elba
    Mismatch detected at: ABLEWE
    Your expression is not a palindrome.
    Enter an expression (or hit Enter to exit) : 
    
    Enter an expression (or hit Enter to exit) : Able were you ere you saw Elba
    Mismatch detected at: Ablewe
    Your expression is not a palindrome.
    Enter an expression (or hit Enter to exit) : 
    
    注意:当前,您正在将转换为大写的用户输入传递给函数,
    isAlindrome
    ,因此输出字符串(
    ABLEWE
    在上面显示的示例运行中)是大写的。更改案例的更好位置是
    isPalindrome
    内部,如下所示:

    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    import java.util.Stack;
    
    public class Palindrome {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            String line;
    
            do {
    
                // prompt user for input String and store it in line
                System.out.print("Enter an expression (or hit Enter to exit) : ");
                line = input.nextLine();
    
                // if user hits Enter or simply types in spaces and hits enter, break out of
                // loop
                if (line.trim().isEmpty()) {
                    break;
                }
    
                // call isPalindrom
                // if it returns true, display one message, else display another
                if (Palindrome.isPalindrome(line)) {
                    System.out.println("Your expression is a palindrome.");
                } else {
                    System.out.println("Your expression is not a palindrome.");
                }
    
            } while (line.length() != 0);
    
            System.out.println("You didn't enter an expression.  Exiting application ...");
        }
    
        public static boolean isPalindrome(String input) {
            // Create a string by replacing all non-letters by empty string
            String allLetters = input.replaceAll("[^\\p{L}]", "");
    
            // convert input String to upper case
            input = input.toUpperCase();
    
            // create Queue and Stack of Characters
            // to store the input String
            Queue<Character> q = new LinkedList<>();
            Stack<Character> s = new Stack();
    
            // temporarily store the individual Characters
            // in input String before they're pushed onto
            // Stack and added to Queue
            Character letter;
    
            // keep track of differences between Characters
            // in Stack and Queue
            int mismatches = 0;
    
            // loop through input String
            for (int i = 0; i < input.length(); i++) {
                // get current Character
                letter = input.charAt(i);
    
                // if the current Character is an alphabetic character
                if (Character.isLetter(letter)) {
                    // push it onto the Stack
                    s.push(letter);
                    // add it to the Queue
                    q.add(letter);
                }
            }
    
            int index = 0;
            // while the Queue isn't empty
            while (!q.isEmpty()) {
                // remove a Character from the Queue
                // pop a Character from the Stack
                // if they're not equal, increment mismatches
                if (!q.remove().equals(s.pop())) {
                    // Stack will produce the input String backwards
                    // Queue will produce the input String forwards
                    mismatches++;
                    System.out.println("Mismatch detected at: " + allLetters.substring(0, index + 1));
                    break;
                }
                index++;
            }
    
            // return true if there are no mismatches, else return false
            return (mismatches == 0);
        }
    }
    

    您有权访问回文.isAlindrome的代码吗?这将使这项任务变得相当容易…是的。我可以访问代码。在
    input
    (0和
    input.size()-1
    )的两端分别启动左和右
    int
    指针。比较左右位置的字符;如果他们不同,你有你的答案。否则,将左指针向右移动一个字母,将右指针向左移动一个字母,然后重复比较。继续这样做,直到您发现差异,或者指针彼此经过(意思是
    输入
    是回文)。您是否有权访问
    回文的代码。isAlindrome
    ?这将使这项任务变得相当容易…是的。我可以访问代码。在
    input
    (0和
    input.size()-1
    )的两端分别启动左和右
    int
    指针。比较左右位置的字符;如果他们不同,你有你的答案。否则,将左指针向右移动一个字母,将右指针向左移动一个字母,然后重复比较。继续这样做,直到您发现差异,或者指针彼此经过(意思是
    input
    是回文)。