This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 12627
50
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...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!