Incident avec Break et switch
Bonjour, je ne comprends pas le comportement de java sur ce point
avec ce code
Code:
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
| import javax.swing.* ;
public class Confirm1
{
public static void main (String [] args)
{
final int NFOIS=5;
int i;
for (i=0;i<NFOIS;i++)
{
int rep= JOptionPane.showConfirmDialog(null, "Voulez-vous continuer ?");
System.out.println("Reponse : " + rep);
if (rep==1)
{
System.out.println ("Votre réponse est " + rep);
break;
}
else System.out.println ("Vous n'avez pas répondu non");
}
System.out.println("Au revoir");
}
} |
si je clique sur "non" c'est à dire rep==1 le programme execute bien le break et sort.
par contre si j'utilise ce nouveau code
Code:
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
| import javax.swing.* ;
public class Confirm1
{
public static void main (String [] args)
{
final int NFOIS=5;
int i;
for (i=0;i<NFOIS;i++)
{
int rep= JOptionPane.showConfirmDialog(null, "Voulez-vous continuer ?");
System.out.println("Reponse : " + rep);
if (rep==1)
{
System.out.println ("Votre réponse est " + rep);
break;
}
else System.out.println ("Vous n'avez pas répondu non");
}
System.out.println("Au revoir");
}
} |
ici si je clique sur "non" rep==1 la boîte de dialogue s'affiche n fois =5 fois donc.
je ne comprends pas. Merci pour votre éclaircissement.