逻辑错误导致语句在 While 循环中打印两次

所以我目前遇到的问题是,在我完成所有步骤后,语句“输入你的命令(反向,先替换,最后替换,全部删除,删除)”打印了两次。


我相信正在发生的是循环执行了两次,但我不知道为什么。任何帮助将不胜感激解决这个问题。如果我的代码格式不好,请提前抱歉,仍在学习如何正确格式化。


import java.util.Scanner;


public class StringChangerenter {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        // Output Variables

        String userInput = "";


        // Variables

        String removeChar = "", removeAllChar = "";

        int removeIndex = 0;


        // First Output

        System.out.println("Enter the string to be manipulated");

        userInput = keyboard.nextLine();

        String command = "";


        // While loop

        while (!command.equalsIgnoreCase("quit")) {

            // Output

            System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove)");

            command = keyboard.nextLine();

            if (command.equalsIgnoreCase("remove")) {

                System.out.println("Enter the character to remove");

                removeChar = keyboard.nextLine();

                int totalCount = 0;

                for (int j = 0; j < userInput.length(); j++) {

                    if (userInput.charAt(j) == removeChar.charAt(0)) {

                        totalCount = totalCount + 1;

                    }

                }

                System.out.println("Enter the " + removeChar

                        + " you would like to remove (Not the index - 1 = 1st, 2 = 2nd, etc.):");

                removeIndex = keyboard.nextInt();

                int currentIndex = 1;

                if (removeIndex <= totalCount) {

                    for (int i = 0; i < userInput.length(); i++) {

                        if (userInput.charAt(i) == removeChar.charAt(0)) {

                            if (currentIndex == removeIndex) {

                                String firstpartOfString = userInput.substring(0, i);

                                String secondpartOfString = userInput.substring(i + 1, userInput.length());

        }

    }

}


泛舟湖上清波郎朗
浏览 131回答 2
2回答

UYOU

处理一个字符后获得两个条目的原因是您没有完全阅读包含该字符的行。具体来说,您keyboard.nextInt();在上分支和keyboard.next();下分支中使用。虽然它们分别读取下一个整数和字符,但它们不处理行尾标记。然后,当您到达循环的顶部时,您调用keyboard.nextLine()which 处理 int (或字符,在删除所有情况下)之后出现的任何字符,直到行标记结束。使用预期的用户输入,这只是一个空字符串。要解决此问题,您需要确保keyboard.nextLine()在仅读取整数或单个字符的情况下通读。

冉冉说

发生的事情是,while循环的条件是while&nbsp;(!command.equalsIgnoreCase("quit"))这在英语中的意思是,只要命令不等于“退出”然后运行这个循环。在循环内部,命令实际上从未设置为“退出”。例如,如果我将输入字符串指定为“abcde”并要求在位置 1 删除“c”。那么您的逻辑将命令设置为“删除”command&nbsp;=&nbsp;keyboard.nextLine();然后将最终值打印为“abde”。现在当循环结束时,命令仍然是“删除”,因此循环再次执行。一种可能的解决方案是明确询问用户是否要使用 do while 循环重试。也只是一个提示,我看到你使用了 nextInt。建议在下一个 int 之后立即使用 nextLine。看到这个的原因:Java Scanner 不等待用户输入如果您想运行更多命令,那么如果您明确征得用户同意,这就是您的代码:public static void main (String[] args) throws java.lang.Exception&nbsp; &nbsp; {&nbsp; &nbsp; Scanner keyboard = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; // Output Variables&nbsp; &nbsp; &nbsp; &nbsp; String userInput = "";&nbsp; &nbsp; &nbsp; &nbsp; // Variables&nbsp; &nbsp; &nbsp; &nbsp; String removeChar = "", removeAllChar = "";&nbsp; &nbsp; &nbsp; &nbsp; int removeIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; // First Output&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the string to be manipulated");&nbsp; &nbsp; &nbsp; &nbsp; userInput = keyboard.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; String command = "";&nbsp; &nbsp; &nbsp; &nbsp; String retry = "";&nbsp; &nbsp; &nbsp; &nbsp; // While loop&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Output&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove)");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command = keyboard.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (command.equalsIgnoreCase("remove")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the character to remove");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeChar = keyboard.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int totalCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < userInput.length(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userInput.charAt(j) == removeChar.charAt(0)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totalCount = totalCount + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the " + removeChar&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " you would like to remove (Not the index - 1 = 1st, 2 = 2nd, etc.):");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeIndex = keyboard.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyboard.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int currentIndex = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (removeIndex <= totalCount) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < userInput.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userInput.charAt(i) == removeChar.charAt(0)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (currentIndex == removeIndex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String firstpartOfString = userInput.substring(0, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String secondpartOfString = userInput.substring(i + 1, userInput.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The new sentence is " + firstpartOfString + secondpartOfString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userInput = firstpartOfString + secondpartOfString;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentIndex = currentIndex + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Can't find " + removeChar + " occuring at " + removeIndex + " int the string.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove All Code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (command.equalsIgnoreCase("remove all")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the character to remove");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeAllChar = keyboard.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String newString = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < userInput.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userInput.charAt(i) != removeAllChar.charAt(0)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newString = newString + userInput.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userInput = newString;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The new sentence is " + userInput);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Do you want to go again?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retry = keyboard.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Bracket for while loop&nbsp; &nbsp; &nbsp; &nbsp; }while("yes".equalsIgnoreCase(retry));&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java