1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| class menu183CORRIGE {
public static void main ( String [] args ){
String[] st1 = {"Partir loin", "rester au lit", "se lever"};
String[] st2 = {"Rouge", "vert", "bleu", "jaune", "violet"};
String[] st1 = {"Canard", "poule", "pintade", "oie", "dinde", "pleiad"};
menu[] mt = new Menu[3];
boolean saisieOk;
try{
mt[0] = new Menu ("Que voulez-vous faire ? ", st1);
mt[1] = new Menu ("Quelle est votre couleur preferee ?", st2);
mt[2] = new Menu ("Trouver l'intrus.", st3);
}catch(Npasbon ex){
}
for (int i = 0; i<5; i++){
do{
try{
saisieOk = true;
mt[i % 3].executerMenu();
}catch(HorsIntervalle ex){
Terminal.ecrireString("Vous devez entrer un");
Terminal.ecrireStringln("des choix proposes");
saisieOk = false;
}catch(PasNombre ex){
Terminal.ecrireString("Vous devez entrer un nombre");
saisieOk = false;
}
}while(!saisieOk);
}
}
}
class NPasBon extends Exception{}
class PasNombre extends Exception{}
class HorsIntervalle extends Exception{}
class Menu{
String[] tabChoix;
String question;
Menu(String q, String[] t) throws NPasBon{
if (t.length > 9){
throw new NPasBon();
}
tabChoix = t;
question = q;
}
void affiche(){
Terminal.ecrireStringln(question);
for(int i=0; i<tabChoix.length; i++){
Terminal.ecrireStringln(tabChoix[i] + ": tapez " + (i+1));
}
Terminal.ecrireString("entrer votre choix");
}
int charToInt(char c) throws PasNombre{
if (c == '1'){
return 1;
}else if (c == '2'){
return 2;
}else if (c == '3'){
return 3;
}else if (c == '4'){
return 4;
}else if (c == '5'){
return 5;
}else if (c == '6'){
return 6;
}else if (c == '7'){
return 7;
}else if (c == '8'){
return 8;
}else if (c == '9'){
return 9;
}else if (c == '0'){
return 0;
}
throw new PasNombre();
}
int saisir () throws PasNombre, HorsIntervalle{
char rep = Terminal.lireChar();
int repInt;
repInt = charToInt(rep);
if ((repInt == 0) | | (repInt > tabChoix.length)){
throw new HorsIntervalle();
}
return repInt;
}
int executerMenu() throws PasNombre, HorsIntervalle{
affiche();
return saisir();
}
} |
Partager