Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in
Sequence
Operations menu
Core exercise
Write menu() that displays a menu (1 addition, 2 subtraction, 3 multiplication, 4 division), reads the choice then two double numbers from the keyboard, and prints the result. Handle division by zero with an error message, without crashing. The tests look for the French labels: the menu must contain Addition, Soustraction, Multiplication and Division, and the error message the word Erreur.
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) {
menu();
}
public static void menu() {
Scanner sc = new Scanner(System.in);
// TODO : afficher le menu, lire le choix, lire a et b, afficher le resultat
}
}
The core exercise is validated in the workshop, which requires JavaScript.
Hint 1
A switch on the choice. For division, test b == 0 before dividing: with double, dividing by zero does not throw an exception but gives Infinity.