File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 13864
    
  10

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: 10905
    
  12

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: 10905
    
  12

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple problem
 
Similar Threads
While Loop Error
Methods
Having problem compiling this answer
Java Help: NullPointerException
Dice Rolling Simulation