始める無料で始める

文字数

ページに印刷する動詞の一覧が与えられています。すべての文字を印刷するのに、プリンターのインクが足りるかを確認したいとします。

この演習はコースの一部です

中級 Java

コースを見る

演習の手順

  • conjugation 配列にいくつの単語があるかを調べます。
  • tabLength超えないようにするため、適切な比較演算子を選びます。
  • 各反復の最後に i を増やすため、適切な更新演算子を選びます。
  • conjugation から各 word を取り出せるように、必要な変数を書きます。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

class CharacterCounter {
  public static void main(String[] args) {
    String[] conjugation = {"hide", "hindered", "hiding", "hidden", "hindering", "hid", "hides", "hinder"};
    // Retrieve the number of words in the array by calling the correct field
    int tabLength = conjugation.____;
    int numberOfCharacters = 0;

    // Choose the appropriate operators (logical and increment)
    for (int i = 0; i ____ tabLength; i____) {
      // Give the proper variable to get each word out of conjugation
      String word = conjugation[____];
      numberOfCharacters += word.length();
    }
    System.out.println("There are " + numberOfCharacters + " characters in the list");
  }
}
コードを編集して実行