• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why wont these nested loops run?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im creating a program that asks the user for a 6 digit number consisting of dice face values, ex: 123456. A die will roll until the pattern 123456 has been rolled in that exact format, but for some reason, my third while loop in the portion of code ive shown wont execute. any ideas? I can show you the entire code if you need it
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way to get out of the second loop is for gotFirst to not be 1. For this to happen line 25 has to be executed and if line 25 is executed, line 26 will be executed as well. So when you come out of the second loop, gotSecond will be 0 and you only enter the third loop if gotSecond is 1.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But i need gotFirst to be 1, the 1's mean that the die rolled onto the first number out of six. so if the die doesnt land on the correct face, it will be given the value 0 and try to get the first number again, starting the process all over.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the rules you're trying to implement there. Okay, I understand the first loop where you roll the dice until you match the first number. But why isn't the second loop the same, so that you roll the dice until you match the second number? Isn't that the rule? If not, then what is the rule?

I have to say that I don't even understand what this means:

A die will roll until the pattern 123456 has been rolled in that exact format



In particular I don't understand what "in that exact format" means.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, let me try to be more specific. If i input that i want 123456 as my 6 digit number, the die will roll until i get a 1. Then on the next roll, if it lands on anything but a 2, everything resets and it has to land on 1 again before going to 2. If the die does land on 2 after a 1, then the next roll must be a 3 or the process resets until the 6th number is reached. But with the code i have now, it never reads the 3 after landing a 1 and 2.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, okay. So if in your second loop you don't roll the second number right away on the first try, then you have to go back and redo the first loop again, right? Your code isn't doing that.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also, for a topic named "Why wont these nested loops run?", it must be pointed out that there isn't a nested loop in the code.

Henry
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Also, for a topic named "Why wont these nested loops run?", it must be pointed out that there isn't a nested loop in the code.

Henry


oops
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a hint: For the second and subsequent rolls you don't need a loop. That's because you can only try each roll once.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i got to it finally check for the entire pattern. But im having trouble getting the program to reset and try to roll for the first number whenever a number doesnt match. Right now, if i want to get 123456 and get a 1 on a roll, itll see if the next 5 numbers match 23456 before resetting. I want it to reset if i roll a 1 and dont get a 2 after.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Hoang wrote:Well i got to it finally check for the entire pattern. But im having trouble getting the program to reset and try to roll for the first number whenever a number doesnt match. Right now, if i want to get 123456 and get a 1 on a roll, itll see if the next 5 numbers match 23456 before resetting. I want it to reset if i roll a 1 and dont get a 2 after.



Then you have a bug in your code. You need to fix that bug if you want it to behave properly.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah thats the problem, i cant seem to figure it out, im not sure if it still does it since i took out the System.out.printlns that showed me how my program was running, but heres my code now.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this:



0 means you haven't got it yet, and 1 means you got it, right? (And if so, don't use ints--use booleans).

So that says "keep going as long as..." what? Describe the condition under which that loop will keep going, based on that line of code. Describe it in English, in terms of the rules of your game, not in terms of java or loops.

Aside from that, and from using ints where you should be using booleans, that code is rather painful to read. The logic is really hard to follow. Too much duplicated code, too much nesting. It would be clearer if you used methods (for less duplicated code and less nesting) and arrays (for less duplicated code).
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes 0 means i havent got the number, 1 means i have. The condition i tried using for that loop is, keep rolling the die if none of the numbers in pattern have been matched. The while condition you quoted may be unneccessary, but i wanted to make sure the game would keep going until the six digit number has been matched. Maybe i could shorten it by making it } while ((gotSixth ==0)); since gotSixth ==1 can only be achieved if all the numbers before it have been matched. Also i know its hard to read, im not allowed to use methods or arrays. This is one game of 6, so i can only have a test and 6 classes for each game.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What dreadful long lines. I have shown you the correct way to divide them, so you can actually read the code.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Hoang wrote:Also i know its hard to read, im not allowed to use methods or arrays...


Hunh? . Must be some new kind of teaching by torture...

All I can say is: once you've finished this exercise, forget it ever existed.

You always want to use methods to break up logic - and, within reason: the more, the better.

Also, that has virtually nothing to do with making your code neat and easy to read.

As for arrays: there are other alternatives, which you'll probably learn about soon; but for a fixed-sized group of values, they are exactly what you should use - and don't let anyone tell you any different.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Hoang wrote:yes 0 means i havent got the number, 1 means i have. The condition i tried using for that loop is, keep rolling the die if none of the numbers in pattern have been matched.



You're contradicting yourself. Earlier you said that as soon as any one digit doesn't match, you have to start over--in other words, if the desired number is 1234, then you have to roll a 1, and the very next roll has to be a 2, and the very next roll after that has to be a 3, and then the very next roll has to be a 4.

What you're saying here, and what that do/while condition says, is keep rolling only if all the numbers are wrong. So if I got a 1 and then a 5, since the 1 matched, the "none have been matched" condition is false, so we break out of the loop.

And if you're really not allowed to use methods, you should drop the class, because the teacher is incompetent. This could be made much easier to understand with proper division of responsibility.
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic