import java.util.*; class Tableaux { static final Scanner input = new Scanner(System.in); /** * Échange les éléments d'indices 'i' et 'j' dans 'tab'. */ static void echanger(int[] tab, int i, int j) { int tmp = tab[i]; tab[i] = tab[j]; tab[j] = tmp; } /** * Retourne le nombre d'occurences de 'valeur' dans 'tab'. */ static int occurences(int[] tab, int valeur) { int compte = 0; for (int i = 0 ; i < tab.length ; i++) if (tab[i] == valeur) compte++; return compte; } /** * Fonction utilisée en cas d'erreur de lecture d'un entier. * Supprime la ligne en cours sur l'entrée. Retourne 0, ou -1 si * la fin de l'entrée est atteinte. */ static int viderEntree() { int res = -1; if (input.hasNext()) { input.next(); // lit le prochain mot... input.nextLine(); // ... et le reste de la ligne res = 0; } return res; } /** * Affiche un menu si 'afficher' est vrai, et attend un choix de * l'utilisateur. Retourne le choix effectué. */ static int menu(boolean afficher) { int choix; if (afficher) { System.out.println(",----[ Menu ]\n" + "| 0 : quitter\n" + "| 1 : afficher le menu\n" + "| 2 : afficher la taille du tableau\n" + "| 3 : afficher le contenu du tableau\n" + "| 4 : modifier un élément\n" + "| 5 : échanger deux éléments\n" + "| 6 : compter une valeur\n" + "`----"); } System.out.print("Choix ? "); if (input.hasNextInt()) { // un entier est dispo sur l'entrée ? choix = input.nextInt(); // oui, le lire } else if (viderEntree() == -1) { // non, est-ce la fin de l'entrée ? choix = 0; } else { // non, c'est un choix invalide choix = -1; } System.err.println("choix : " + choix); return choix; } /** * Affiche le tableau 'tab'. */ static void afficherTableau(int[] tab) { for (int i = 0 ; i < tab.length ; i++) { if (i == 0) System.out.print("[ " + tab[0]); else System.out.print(", " + tab[i]); } System.out.println(" ]"); } /** * Modifie un élément de 'tab'. L'indice et la valeur sont * demandés à l'utilisateur. */ static void modifierValeur(int[] tab) { System.out.println("> indice de l'élément à modifier ?"); // NB : i < 0 en cas d'erreur de lecture int i = input.hasNextInt() ? input.nextInt() : viderEntree() - 1; if (i >= 0 && i < tab.length) { System.out.println("> nouvelle valeur ?"); if (input.hasNextInt()) { int v = input.nextInt(); System.out.println("> modification : [" + i + "] <- " + v); tab[i] = v; } else { System.out.println(" ERREUR : entrée invalide."); viderEntree(); } } else { System.out.println("> ERREUR : indice invalide."); } } /** * Échange les valeurs de deux éléments de 'tab'. Les indices * sont demandés à l'utilisateur. */ static void echangerValeurs(int[] tab) { System.out.println("> indices des 2 éléments à échanger ?"); // NB : i, j < 0 en cas d'erreur de lecture int i = input.hasNextInt() ? input.nextInt() : viderEntree() - 1; int j = input.hasNextInt() ? input.nextInt() : viderEntree() - 1; if (i >= 0 && i < tab.length && j >= 0 && j < tab.length) { System.out.println("> échange: [" + i + "] <-> [" + j + "]"); echanger(tab, i, j); } else { System.out.println("> ERREUR : indice invalide."); } } static void rechercherValeur(int[] tab) { System.out.println("> valeur à rechercher ?"); if (input.hasNextInt()) { int v = input.nextInt(); System.out.println("> recherche: " + v); System.out.println("COMPTE = " + occurences(tab, v)); } else { System.out.println(" ERREUR : entrée invalide."); viderEntree(); } } /** * Programme principal. */ public static void main(String[] args) { final int taille = 20; // taille du tableau int[] tab = new int[taille]; // initialisation du tableau for (int i = 0 ; i < tab.length ; i++) tab[i] = i; boolean afficher_menu = true; int choix; do { choix = menu(afficher_menu); afficher_menu = false; switch (choix) { case 0: System.out.println("\nBaille"); break; case 1: afficher_menu = true; break; case 2: System.out.println("> TAILLE = " + tab.length); break; case 3: afficherTableau(tab); break; case 4: modifierValeur(tab); break; case 5: echangerValeurs(tab); break; case 6: rechercherValeur(tab); break; default: System.out.println("> ERREUR : choix invalide."); break; } } while (choix != 0); } }