• Post Reply Bookmark Topic Watch Topic
  • New Topic

infinite loop  RSS feed

 
Mahesh Mire
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was learning java from head first java and tried to code the guess game on my own but I am stuck in an infinite loop during the runtime. I suspect it has something to do with my assignment of boolean. I am attaching the
main game class (mg.class). Please help me find the error. Also is there anything one can do to terminate an infinite loop runtime without closing the command prompt?? Thanks in advance.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahesh. Welcome to the Ranch!

It's a lot easier to tell what's going on in your code if you add some consistent indentation. I've done that for you this time.

Your loop will end once w1 or w2 is false. Which will happen once g1 or g2 equals n. So...where do you ever change g1 or g2? They are initialised to zero, and never change from that.

(By the way - your if statement on line 26 is completely unnecessary - if w1 or w2 is false you'll be leaving the loop immediately at that point anyway_.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Mire wrote:

This (and the p2 equivalent) are the problem. Which way is that assignment supposed to go? What variable gets assigned what value, and what did you intend to assign?
 
Mahesh Mire
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Hi Mahesh. Welcome to the Ranch!

It's a lot easier to tell what's going on in your code if you add some consistent indentation. I've done that for you this time.

Your loop will end once w1 or w2 is false. Which will happen once g1 or g2 equals n. So...where do you ever change g1 or g2? They are initialised to zero, and never change from that.

(By the way - your if statement on line 26 is completely unnecessary - if w1 or w2 is false you'll be leaving the loop immediately at that point anyway_.

Doesnt line 12 and 14 acomplish that??. Yes I realise line 26 is unnescessary but it was a result of me tweaking the program that i forst wrote where while loop had a condition as true.
 
Mahesh Mire
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
Mahesh Mire wrote:

This (and the p2 equivalent) are the problem. Which way is that assignment supposed to go? What variable gets assigned what value, and what did you intend to assign?

Oh yes. thanks a lot. anyways is there anythimg one can do to abort an infinite loop during runtime??
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Mire wrote:Oh yes. thanks a lot. anyways is there anythimg one can do to abort an infinite loop during runtime??
Ctrl-C?
 
Mahesh Mire
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Mahesh Mire wrote:Oh yes. thanks a lot. anyways is there anythimg one can do to abort an infinite loop during runtime??
Ctrl-C?

Yes that works .thank you all for helping me.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy to be of help. Welcome to the ranch
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!