my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Simple problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple problem" Watch "Simple problem" New topic
Author

Simple problem

Arash Ehsanfar
Greenhorn

Joined: Jun 20, 2012
Posts: 4
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Arash Ehsanfar
Greenhorn

Joined: Jun 20, 2012
Posts: 4
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

Joined: Oct 02, 2003
Posts: 11175
    
  16

Did you try Jesper's suggestion?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Arash Ehsanfar
Greenhorn

Joined: Jun 20, 2012
Posts: 4
he did point out the problem, however, he did not suggest how i could solve it.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
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

Joined: Oct 02, 2003
Posts: 11175
    
  16

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.
 
Consider Paul's rocket mass heater.
 
subject: Simple problem
 
Similar Threads
While Loop Error
Methods
Having problem compiling this answer
Java Help: NullPointerException
Dice Rolling Simulation