Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in

Sequence

Password (do…while)

Core exercise

Write demanderMotDePasse() that asks again for the password java123 as long as it is wrong, with a do…while loop, then returns the number of attempts it took.

A workshop goes here: you write and run code in it. It requires JavaScript.

Starting code

Exercices.java

import java.util.Scanner;

public class Exercices {

    public static void main(String[] args) {
        int essais = demanderMotDePasse();
        System.out.println("Trouve en " + essais + " essai(s)");
    }

    public static int demanderMotDePasse() {
        Scanner sc = new Scanner(System.in);
        // TODO : do { ... } while (mot de passe faux); puis renvoyer le nombre d'essais
        return 0;
    }
}

The core exercise is validated in the workshop, which requires JavaScript.

Hint 1

It is the textbook case for do…while: ask first, test afterwards. Compare strings with equals, never with ==.