• Post Reply Bookmark Topic Watch Topic
  • New Topic

for loop brain cramp!  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to create a dice rolling program. I want users to be able to enter number of dice to roll, then type of dice to roll i.e. d6, d20, etc. I need the variable for the die roll result to be added up and displayed to the user in a text box. This is the code I have to do it:

public void doDiceStuff()
{
dice = new DieRoller();
String input_1 = textIn_1.getText();
String input_2 = textIn_2.getText();
int qty = Integer.parseInt(input_1);
int dieType = Integer.parseInt(input_2);
for (i = 0; i <= qty; i++) {
int newValue = newValue + dice.rollIt2(dieType);
}
textOut_0.setText("" + newValue);
}
I get an error on the last line: "Cannot find symbol - variable newValue"

Thank you for your help!

~Howard
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you declare your newValue variable inside the loop. That means it falls out of scope when you exit the loop. you need to do this:

int newValue = 0;

for (whatever) {
newValue += dice.rollIt2(dieType);
}
etc...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!