• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for loop brain cramp!

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Beware the other head of science - it bites! Nibble on this message:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic