214k views
1 vote
In Java I’m having trouble with variables. When I try to call a variable from my while loop it says, “finger cannot be resolved to a variable.” The error is on line 49.

In Java I’m having trouble with variables. When I try to call a variable from my while-example-1
User BGerrissen
by
3.9k points

1 Answer

8 votes

You're declaring variable finger inside your while loop so it only exists inside your while loop. You're then trying to use your finger variable outside your while loop, which is impossible because finger only exists inside your while loop.

Try declaring total at the beginning of your program, adding finger to total before you break from your while loop and then adding randNum to your total at the end.

To better understand why you can't use finger outside your while loop, look up "java scope". This will explain why you can only use variable in the regions they're declared in.

User Arseniy Krupenin
by
4.6k points