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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何顛倒短語(yǔ)中的單詞順序和顛倒單詞中的字母?

如何顛倒短語(yǔ)中的單詞順序和顛倒單詞中的字母?

千巷貓影 2023-11-01 16:36:32
兩個(gè)問(wèn)題:在用戶輸入的短語(yǔ)(表示兩個(gè)或多個(gè)單詞之間有空格的短語(yǔ))中,僅最后兩個(gè)單詞被反轉(zhuǎn),短語(yǔ)中的其他單詞不會(huì)被反轉(zhuǎn)甚至打印。我的用于反轉(zhuǎn)單詞中字母順序的代碼(單詞表示單個(gè)單詞)似乎沒(méi)有打印任何內(nèi)容,但它無(wú)休止地接受輸入而沒(méi)有結(jié)果。重要提示:我被禁止使用 StringBuilder、數(shù)組或任何其他“高級(jí)”工具。事實(shí)上,我最好只使用 AP 計(jì)算機(jī)科學(xué)指南中引用的方法(盡管不是必需的)。我嘗試了很多事情,包括調(diào)整參數(shù)、不同的串聯(lián)等。我希望“這是一個(gè)字符串”的輸出是“字符串 a 是這個(gè)”。相反,它打印“字符串 a”。我希望“heck”的輸出是“kceh”。相反,我什么也沒(méi)得到。注意:代碼中的注釋是我的額外關(guān)注點(diǎn)和問(wèn)題,也旨在幫助更好地理解我的代碼。抱歉,如果有點(diǎn)混亂,這是我第一次真正評(píng)論自己的代碼。 Scanner userInput = new Scanner(System.in);    System.out.print("Enter a word or phrase: ");    String str = userInput.nextLine();  //user-input string     String reversePhrase = "";  //placeholder for reversed phrase    String reverseWord = "";    //placeholder for reversed word    char reverseLetter = ' ';   //placeholder for letters in reversed word    for(int position = 0; position < str.length(); position++)    {        if(str.indexOf(" ") > -1)   //checks for space in the user-input string        {            while(str.indexOf(" ") > -1)            {                reversePhrase = str.substring(0, str.indexOf(" ")); //this might be the problem, but i'm stuck on any solutions                str = str.substring(1 + str.indexOf(" "));                reversePhrase = str + " "+ reversePhrase;            }            System.out.println(reversePhrase);  //only reverses and prints last two words in phrase        }        else if(!(str.indexOf(" ") > -1))   //if there's no space in the user-input string        {            for(position = str.length() - 1; position >= 0; position --)    //does this conflict with the "for" parameter above?            {                while(position >= 0)    //wasn't sure what to put for this parameter                {                    reverseLetter = str.charAt(position);                    reverseWord = reverseLetter + reverseWord;                }            }            System.out.println(reverseWord);        }    }
查看完整描述

3 回答

?
米脂

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊

編寫代碼時(shí),始終嘗試遵循 KISS 原則。保持簡(jiǎn)單愚蠢。您迷失在嵌套的 for-if while 循環(huán)中,很難找出哪里出了問(wèn)題。


另一個(gè)原則是:不要讓多個(gè)任務(wù)使您的方法超載。使用小而簡(jiǎn)單的方法,一次只執(zhí)行一項(xiàng)任務(wù)。例如,下面我已將reversePhrase和reverseWord放入自己的方法中。這可以幫助您創(chuàng)建一個(gè)干凈的 main 方法。


public static void main(String args[]) {

    Scanner userInput = new Scanner(System.in);

    System.out.print("Enter a word or phrase: ");

    String str = userInput.nextLine();


    //if input contains spaces call reversePhrase otherwise reverseWord

    //str.contains(" ") is IMO cleaner, but you can change it to str.indexOf(" ") > -1 if you find it better

    if(str.contains(" ")){

        System.out.println(reversePhrase(str));

    }

    else{

        System.out.println(reverseWord(str));

    }

}


private static String reverseWord(String input) {

    String result = "";

    for(int i = input.length()-1; i >= 0; i--){

        result = result + input.charAt(i);

    }

    return result;

}


private static String reversePhrase(String input) {

    String result = "";

    while(input.contains(" ")){

        result = result + input.substring(input.lastIndexOf(" ")+1) + " ";

        input = input.substring(0, input.lastIndexOf(" "));

    }

    return result + input;

}


查看完整回答
反對(duì) 回復(fù) 2023-11-01
?
慕尼黑的夜晚無(wú)繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

在你的 while 循環(huán)中:


while(position >= 0){    //wasn't sure what to put for this parameter

     reverseLetter = str.charAt(position); // position stays the same

     reverseWord = reverseLetter + reverseWord;

}

它不會(huì)改變 的值position。(position永遠(yuǎn)不會(huì)是 0)我建議position--在最后添加,如下所示:


while(position >= 0){    //wasn't sure what to put for this parameter

     reverseLetter = str.charAt(position); // position stays the same

     reverseWord = reverseLetter + reverseWord;

     position--;

}

它會(huì)改變變量的值position。


另外,您的代碼中有一if和一。else if我建議更改else if為,else因?yàn)檫@樣做毫無(wú)意義:


boolean randomBoolean = new java.util.Random().nextBoolean();

if(randomBoolean){...}

else if(!randomBoolean){...} // If randomBoolean == false, then this code will execute anyway



查看完整回答
反對(duì) 回復(fù) 2023-11-01
?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊

我已經(jīng)刪除了其中一個(gè)for循環(huán),因?yàn)槟恍枰?。另外,while針對(duì)一個(gè)單詞的情況進(jìn)行循環(huán)。對(duì)于第一種情況,您可以使用另一個(gè)字符串來(lái)臨時(shí)保存最后一個(gè)單詞。結(jié)果如下:


public static void main(String[] args) {

    Scanner userInput = new Scanner(System.in);


    System.out.print("Enter a word or phrase: ");

    String str = userInput.nextLine();  //user-input string


    String reversePhrase = "";  //placeholder for reversed phrase

    String reverseWord = "";    //placeholder for reversed word

    char reverseLetter;   //placeholder for letters in reversed word

    final String space = " ";


    if(str.contains(space)) {

        while(str.contains(space))

        {

            int i = str.lastIndexOf(space);

            String lastWord = str.substring(i);

            str = str.substring(0, i);

            reversePhrase += lastWord;

        }

        //We add the first word

        reversePhrase = reversePhrase + space + str;

        System.out.println(reversePhrase.trim());

    }

    else {

        for(int position = str.length() - 1; position >= 0; position --) {

            reverseLetter = str.charAt(position);

            reverseWord =  reverseWord + reverseLetter;

        }

        System.out.println(reverseWord);

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-11-01
  • 3 回答
  • 0 關(guān)注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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