Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple problem

 
Arash Ehsanfar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Can anybody help me with below problem.

once i run the program, position1 is always equal to dice3.

isn't position1 supposed to add the value of dice3 as long as it is going through the loop???


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You're declaring and initializing the variable position1 inside the loop, in line 17. Every time the loop executes, the variable is created and initialized anew. It doesn't remember what happened the last time the loop executed.

Put line 17 outside the loop - before line 9.
 
Arash Ehsanfar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you advice me on how i could have a variable that stores the value of dice3 every time it is thrown?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try Jesper's suggestion?

 
Arash Ehsanfar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
he did point out the problem, however, he did not suggest how i could solve it.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arash Ehsanfar wrote:...however, he did not suggest how i could solve it.

He did...

position1 is the answer of your question...try to understand the flow of your program.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arash Ehsanfar wrote:he did point out the problem, however, he did not suggest how i could solve it.

he didn't? Then what is this?
Jesper de Jong wrote:Put line 17 outside the loop - before line 9.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic