第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

邏輯錯誤導(dǎo)致語句在 While 循環(huán)中打印兩次

邏輯錯誤導(dǎo)致語句在 While 循環(huán)中打印兩次

泛舟湖上清波郎朗 2022-07-20 10:31:16
所以我目前遇到的問題是,在我完成所有步驟后,語句“輸入你的命令(反向,先替換,最后替換,全部刪除,刪除)”打印了兩次。我相信正在發(fā)生的是循環(huán)執(zhí)行了兩次,但我不知道為什么。任何幫助將不勝感激解決這個問題。如果我的代碼格式不好,請?zhí)崆氨?,仍在學習如何正確格式化。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());        }    }}
查看完整描述

2 回答

?
UYOU

TA貢獻1878條經(jīng)驗 獲得超4個贊

處理一個字符后獲得兩個條目的原因是您沒有完全閱讀包含該字符的行。

具體來說,您keyboard.nextInt();在上分支和keyboard.next();下分支中使用。雖然它們分別讀取下一個整數(shù)和字符,但它們不處理行尾標記。

然后,當您到達循環(huán)的頂部時,您調(diào)用keyboard.nextLine()which 處理 int (或字符,在刪除所有情況下)之后出現(xiàn)的任何字符,直到行標記結(jié)束。使用預(yù)期的用戶輸入,這只是一個空字符串。

要解決此問題,您需要確保keyboard.nextLine()在僅讀取整數(shù)或單個字符的情況下通讀。


查看完整回答
反對 回復(fù) 2022-07-20
?
冉冉說

TA貢獻1877條經(jīng)驗 獲得超1個贊

發(fā)生的事情是,while循環(huán)的條件是

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

這在英語中的意思是,只要命令不等于“退出”然后運行這個循環(huán)。

在循環(huán)內(nèi)部,命令實際上從未設(shè)置為“退出”。例如,如果我將輸入字符串指定為“abcde”并要求在位置 1 刪除“c”。那么您的邏輯將命令設(shè)置為“刪除”

command = keyboard.nextLine();

然后將最終值打印為“abde”?,F(xiàn)在當循環(huán)結(jié)束時,命令仍然是“刪除”,因此循環(huán)再次執(zhí)行。

一種可能的解決方案是明確詢問用戶是否要使用 do while 循環(huán)重試。也只是一個提示,我看到你使用了 nextInt。建議在下一個 int 之后立即使用 nextLine??吹竭@個的原因:Java Scanner 不等待用戶輸入

如果您想運行更多命令,那么如果您明確征得用戶同意,這就是您的代碼:

public static void main (String[] args) throws java.lang.Exception

    {

    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 = "";

        String retry = "";

        // While loop

        do {

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

                keyboard.nextLine();

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

                                System.out.println("The new sentence is " + firstpartOfString + secondpartOfString);

                                userInput = firstpartOfString + secondpartOfString;

                                break;

                            } else {

                                currentIndex = currentIndex + 1;

                            }

                        }

                    }

                } else {

                    System.out.println("Can't find " + removeChar + " occuring at " + removeIndex + " int the string.");

                }

                // Remove All Code


            } else if (command.equalsIgnoreCase("remove all")) {

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

                removeAllChar = keyboard.next();

                String newString = "";

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

                    if (userInput.charAt(i) != removeAllChar.charAt(0)) {

                        newString = newString + userInput.charAt(i);

                    }

                }

                userInput = newString;

                System.out.println("The new sentence is " + userInput);


            }

            System.out.println("Do you want to go again?");

            retry = keyboard.nextLine();

            // Bracket for while loop

        }while("yes".equalsIgnoreCase(retry));

    }


查看完整回答
反對 回復(fù) 2022-07-20
  • 2 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號