Salut!
Tu trouveras ci-dessous un petit exemple de 'switch' avec une lecture de caractères au clavier.
Pour plus d'infos sur les caractères, je te conseille de jeter un oeil dans la classe java.lang.Character
import java.io.IOException;
/**
* Created on 10-févr.-07
*
* @author: HackTrack (philippe.fery@gmail.com)
*/
public class SwitchDemo {
public static final char[] VOYELLES = { 'a', 'e', 'i', 'o', 'u', 'y' };
public static final char[] CONSONNES = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x',
'z' };
public static final char QUIT_CHARACTER = '&';
private static final int TYPE_VOYELLE = 1;
private static final int TYPE_CONSONNE = 2;
private static final int AUTRE_TYPE = 3;
private boolean isRunning;
public SwitchDemo() {
super();
}
public int getSymbolType(char c) {
for (int i = 0; i < VOYELLES.length; i++) {
if (c == VOYELLES[i])
return TYPE_VOYELLE;
}
for (int i = 0; i < CONSONNES.length; i++) {
if (c == CONSONNES[i])
return TYPE_CONSONNE;
}
return AUTRE_TYPE;
}
public void startListeningKeyboard() throws IOException {
isRunning = true;
System.out.println("Appuyer sur '"+QUIT_CHARACTER+"' pour quitter");
while (isRunning) {
char c = (char) System.in.read();
if (Character.getType(c) != 15) {//Le caractère de retour à la ligne est ignoré
switch (getSymbolType(c)) {
case TYPE_VOYELLE:
System.out.println("Le caractère '" + c + "' est une voyelle");
break;
case TYPE_CONSONNE:
System.out.println("Le caractère '" + c + "' est une consonne");
break;
case AUTRE_TYPE:
System.out.println("Le caractère '" + c + "' n'est ni une voyelle ni une consonne");
if (c == QUIT_CHARACTER)
isRunning = false;// condition d'arrêt de la boucle
break;
}
}
}
}
public static void main(String[] args) {
SwitchDemo demo = new SwitchDemo();
try {
demo.startListeningKeyboard();
} catch (IOException e) {
System.out.println("Une erreur est survenue lors de la lecture clavier");
}
System.out.println("Lecture clavier terminée");
}
}
;-)
HackTrack