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

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

有沒(méi)有辦法 println 枚舉和列出字符串中的單詞?

有沒(méi)有辦法 println 枚舉和列出字符串中的單詞?

長(zhǎng)風(fēng)秋雁 2022-11-02 15:58:36
我正在嘗試編寫(xiě)一個(gè) java 程序,該程序?qū)⒂?jì)算聲明的句子中的單詞數(shù),然后將句子分解為單詞,以便列出具有數(shù)值的單詞并顯示單詞。我已經(jīng)解決了總數(shù),但我似乎無(wú)法分解句子中的單詞然后按時(shí)間順序列出它們。我可以用字符做到這一點(diǎn),但不能用文字。我已經(jīng)探索了 Java Cookbook 和其他地方以找到解決方案,但我只是不太了解它。正如我所說(shuō),我可以讓字符計(jì)數(shù),我可以計(jì)算單詞,但我不能讓單個(gè)單詞在單獨(dú)的行上打印,并在字符串中使用數(shù)值來(lái)表示它們的計(jì)數(shù)。public class MySentenceCounter {    public static void main(String[] args) {        String sentence = "This is my sentence and it is not great";        String[] wordArray = sentence.trim().split("\\s+");        int wordCount = wordArray.length;        for (int i=0; i < sentence.length(  ); i++)            System.out.println("Char " + i + " is " + sentence.charAt(i));         //this produces the character count but I need it to form words, not individual characters.        System.out.println("Total is " + wordCount + " words.");    }}預(yù)期結(jié)果應(yīng)如下所示:1 This2 is3 my4 sentence5 and6 it7 is8 not9 greatTotal is 9 words.
查看完整描述

3 回答

?
繁星點(diǎn)點(diǎn)滴滴

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

迭代wordArray您創(chuàng)建的變量,而不是sentencefor 循環(huán)中的原始字符串:


public class MySentenceCounter {

  public static void main(String[] args) {

    String sentence = "This is my sentence and it is not great";

    String[] wordArray = sentence.trim().split("\\s+");

    // String[] wordArray = sentence.split(" "); This would work fine for your example sentence

    int wordCount = wordArray.length;

    for (int i = 0; i < wordCount; i++) {

      int wordNumber = i + 1;

      System.out.println(wordNumber + " " + wordArray[i]);

    }

    System.out.println("Total is " + wordCount + " words.");

  }

}

輸出:


1 This

2 is

3 my

4 sentence

5 and

6 it

7 is

8 not

9 great

Total is 9 words.


查看完整回答
反對(duì) 回復(fù) 2022-11-02
?
飲歌長(zhǎng)嘯

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

盡量避免過(guò)于復(fù)雜,下面的就行了


public class MySentenceCounter {

    public static void main(String[] args) {

        String sentence = "This is my sentence and it is not great";

        int ctr = 0;

        for (String str : sentence.trim().split("\\s+")) {

            System.out.println(++ctr + "" + str) ;

         } 

         System.out.println("Total is " + ctr + " words.");

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-11-02
?
qq_花開(kāi)花謝_0

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

使用 IntStream 而不是 for 循環(huán)的更優(yōu)雅的解決方案:


import java.util.stream.IntStream;


public class ExampleSolution

{

    public static void main(String[] args)

    {

        String sentence = "This is my sentence and it is not great";


        String[] splitted = sentence.split("\\s+");

        IntStream.range(0, splitted.length)

                .mapToObj(i -> (i + 1) + " " + splitted[i])

                .forEach(System.out::println);


        System.out.println("Total is " + splitted.length + " words.");

    }

}


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

添加回答

舉報(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)