do while problem / symbol not recognized
Hi
I'm a newbie to programming and I'm encountering issues with the following program.
The compiler tells me that the variable after the while is not recognized.
What I especially don't understand is that the variable is defined and accepted in the character input and the swicth but later on is not recognized. I'm sure of this because I wrote the entire program, and then compiled it afterwards. I was therefore able to see the compiler advancing as the code was corrected
The error message is "cannot find symbol - variable choix"
Sorry for the french, I'm in a french programming class :)
Code Java:
do {
System.out.println ("1); Acheter Patons ");
System.out.println (" 2); Fabriquer baguettes ");
System.out.println (" 3); Vendre baguette ");
System.out.println (" q ou Q); Quitter ");
char choix = Clavier.lireChar();
switch (choix) {
//demande le nombre de patons a achete
case 1:
System.out.println (" Nombre de patons :");
nombreDePatons = Clavier.lireDouble();
while (nombreDePatons <= 0 ){
System.out.println (" Erreur : choix non valide! ");
}
patons = patons + nombreDePatons;
accumulateurPatons = accumulateurPatons + nombreDePatons;
break;
// demande le nombre de baguette à produire
case 2:
System.out.println (" Nombre de baguette : ");
nombreDeBaguettes = Clavier.lireDouble();
while (nombreDeBaguettes <= 0 ){
System.out.println (" Erreur : choix non valide! ");
}
if (nombreDeBaguettes > nombreDePatons) {
System.out.println ("Pas de patons, pas de baguette! ");
} else if (nombreDeBaguettes <= nombreDePatons) {
baguette = baguette + nombreDeBaguettes;
patons = patons - nombreDeBaguettes;
}
break;
// demande le nombre de baguette à vendre
case 3:
System.out.println (" Nombre de baguette : ");
baguetteAVendre = Clavier.lireDouble ();
while (baguetteAVendre <= 0 ){
System.out.println (" Erreur : choix non valide! ");
}
if (baguetteAVendre > nombreDeBaguettes) {
System.out.print ("Pas de baguette, pas de ventes! ");
} else if (baguetteAVendre <= nombreDeBaguettes) {
baguette = baguette - baguetteAVendre;
accumulateurBaguette = accumulateurBaguette + baguetteAVendre;
}
default:
System.out.println (" Erreur : choix non valide! ");
}
} while (choix != 'q' && choix != 'Q') ;
Thanks in advance
Christian
Re: do while problem / symbol not recognized
Hi! Welcome to the forum! Please read Announcements - What's Wrong With My Code?
Quote:
The error message is "cannot find symbol - variable choix"
it is because you do the declaration of variable inside your do loop statement, and that is the reason why it is not recognized.
those variables that declares inside a block statement are block variables and is not recognizable outside the block.
for example:
Code java:
for(int i = 0; i < 2; i++) {
int b = i; // declaration of b
// and this variable b is recognizable only inside this loop
}
System.out.println(b); // will cause a compilation error because b cannot be found
the above code will make a compilation error because the print statement is trying to access a variable that is declared inside a block statement, and because it is declared inside the block statement, it is only recognized inside the block statement.
in your case, you declared your variable inside a block statement (a do while loop), therefore, when you try to access/read it outside that block (in while statement) it causes a compilation error symbol not found.
and if you ask why since that while statement is incorporate with do while loops so it should be recognized.
the answer is, look at the brackets in do statements, the variable is accessible only within that brackets, while statement of do while loop lies outside that brackets
Re: do while problem / symbol not recognized