08-12-2007, 06:04 AM | #1 | ||
"Dutch"
Join Date: Oct 2000
Location: Tampa, FL
|
Java Question - do while loop problem/help
I have a question about do-while loops using Java and would like to tap some of the resident coders for assistance. Just a nudge in the right direction would be helpful, it's for my classwork I do after work.
I will try to be as articulate as I can. Thanks for any help! Before the code you see starts, there is more code that I input batting stats that allows me to calculate things like batting averages and slugging percentage. Basically, I am making a calculator, I left out all the other stuff, because I think this is all that is important to the problem I am having. Anyway, instead of re-running the program and re-inputting the batting stats, I want the program to loop back to the menu or exit. So I wanted my "do {" loop to start just before "//get menu choice". I chose to make a switch/case menu which seemed perfect for this. The problem seems to be that the "do while" loop does not recognize the variable "formulas" that is created in the switch menu. If I keep the start of the do-while loop where it is (inside the switch menu after int formulas is created), it works like a champ--read: if I choose to exit (formulas=6) it exits, and if I choose the other menu choices it infinately loops (as expected). If anybody has any clues as to how to get my do-while loop to work, I'd appreciate it. Code:
If I start the do-while loop at the commented out starting point before the menu, I get this error. Code:
Last edited by Dutch : 08-12-2007 at 06:06 AM. |
||
08-12-2007, 07:02 AM | #2 |
High School Varsity
Join Date: Jul 2003
|
It's a scoping issue (as you knew I think).
In the scenario where it isn't compiling, you're creating 'formulas' inside the loop. The "while (formulas != 6)" line is technically outside the loop and therefore can't see the formulas var because it is destroyed at the end of the loop. To do what you want, you need to make sure formulas is created outwith the loop. so... change this line Code:
Code:
and decalre formulas above the loop: Code:
|
08-12-2007, 08:55 AM | #3 |
"Dutch"
Join Date: Oct 2000
Location: Tampa, FL
|
Wow, that was exactly it. When I tried that before I had...
Code:
That redeclaration caused this Code:
I just didn't wrap my mind around what was wrong and I struggled with this for quite a while. Thanks for the assistance! |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
|
|