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 Im 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 4 class=

Respuesta :

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.

ACCESS MORE