CommencerCommencer gratuitement

Caractères

On vous a donné une liste de verbes à imprimer sur une page, et vous voulez savoir s’il y a assez d’encre dans votre imprimante pour imprimer tous les caractères.

Cet exercice fait partie du cours

Java intermédiaire

Afficher le cours

Instructions

  • Trouvez combien de mots se trouvent dans le tableau conjugation.
  • Choisissez l’opérateur de comparaison approprié pour vous assurer que l’on reste en dessous de tabLength.
  • Sélectionnez l’opérateur de mise à jour adapté pour augmenter i à la fin de chaque itération.
  • Indiquez la variable afin d’obtenir chaque word de conjugation.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

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");
  }
}
Modifier et exécuter le code