Menu
CoddyTech

テキスト入力と格納

CoddyのJavaジャーニー「ロジックとフロー」セクションの一部。レッスン 50/59。

challenge icon

チャレンジ

簡単

ユーザーからテキストを読み取り、2次元配列に格納するプログラムを作成しましょう。テキストには、ピリオドで区切られた複数の文が含まれる場合があります。各単語を「Word[row,col]: word」の形式で出力してください。

単語を2次元配列に格納するには、各文を単語に分割する必要があります。

String[][] textArray = new String[sentences.length][];
for (int i = 0; i < sentences.length; i++) {
   textArray[i] = sentences[i].trim().split(" ");
}

自分で試してみよう

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();

        String[] sentences = text.split("\\.");
        String[][] textArray = new String[sentences.length][];

        // ここにコードを書いてください
    }
}

ロジックとフローのすべてのレッスン

自分で練習してみよう: Javaオンラインコンパイラ