CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 44
  1. #16
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Bagels(fermi,pico,bagels) code help

    Quote Originally Posted by petes1234
    ... I've been taught to always have the condition check at the top of the loop (though to be honest, I'm not sure why,... probably easier to read).
    In purely practical terms, the idea is that you put the loop condition at the start if the loop can have zero or more iterations, and you put it at the end if there must always be at least one iteration. This way the code structure explicitly mirrors the requirement.

    Having said that, the vast majority of the code I see uses the 'for' loop whatever the situation... it seems that most coders find it easier to follow if the same control structure is used for every loop rather than changing the control structure to reflect the flow.

    The key to performance is elegance, not batallions of special cases...
    J. Bently & D. McIlroy
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  2. #17
    Join Date
    Apr 2007
    Posts
    33

    Re: Bagels(fermi,pico,bagels) code help

    i'm stuck, these are the things i have accomplished:

    generate random number (no digits repeat)

    get user's guess(no digits repeat or tryagain message)

    evaluate guess( i get wether winning,fermi, pico,bagels)


    My problem write now is with the loop i need to write so that after they enter some digits if not in correct places (fermi,pico) the game asks wether you want to keep playing and keeps the same generated number while they keep guessing.

    It also needs to give the option to quit if not winning guess and the game needs to repeat as long as the user wants to keep playing.I've been fighting with this assignment for over a week any help you can give me will be appreciated.

  3. #18
    Join Date
    Apr 2007
    Posts
    33

    Re: Bagels(fermi,pico,bagels) code help

    How do i go about making the program so that when a user is aked if he would like to play again Yes or No, If the user types Yes the game continues if NO the game ends?

  4. #19
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Bagels(fermi,pico,bagels) code help

    Code:
    see uses the 'for' loop whatever the situation... it seems that most coders find it easier to follow
    Actually that has historical significance from the earliest days of "C" when it was specifically implemented on the DEC PDP-11 line of computers.

    The "for" instruction was the most optimized and the various forms could often save 1 instruction over counterparts.

    btw: This is also the reason for the pre-increment preference (when there is no associated assignment). It made use of a special hardware instruction. IIRC this saved 2 machine instructions....

    So "tons" of code was written this way. The "first generation" of "C" developers passed it on, and it remains with us today, even though modern compilers [and vastly increased computer capabilities] render such points moot
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #20
    Join Date
    Apr 2007
    Posts
    33

    Re: Bagels(fermi,pico,bagels) code help

    Why is this method giving me a message unexpected type?It's pointing at my while statement.
    Code:
    public void playGame() 
     {
        Scanner input = new Scanner(System.in);
        System.out.print("Want to play?  " + "Type Y for Yes or N for No and press [Enter]: ") ;
        String answer = input.next();
       while(answer.equalsIgnoreCase("Y") || endGame = false)
       {
       getGuess();
       
       evalGuess(); 
          
       gameStatus() ;
       }
      
       
      if(endGame = true)
       System.out.println("guess =  " +playGuess+  ","+"tries = " +  guessCnt + "  Do you want to play again? Y/N and press [Enter] ") ;
    Last edited by Strobe; April 9th, 2007 at 12:19 PM.

  6. #21
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Bagels(fermi,pico,bagels) code help

    You been asked twice already to post code using code tags and you have been given a link explaining how to do it. I suggest you start post correctly or people may just get fed up of trying to help you.

    We help out here for free in our own spare time. If you can give me one good reason why I should waste my valuable time trying to understand your unformatted code then I will gladly do it.

  7. #22
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Bagels(fermi,pico,bagels) code help

    the line:
    Code:
    while(answer.equalsIgnoreCase("Y") || endGame = false )
    is a problem because a single "=" sets endGame to false. I don't think that this is what you want. Better to use "==". Better still to use no = sign. Since it is a boolean, it can stand on its own in a while() or if() condition.

    And I second that using code tags recommendation. Good code formatting is a sign of respect to those of us who read your code.

  8. #23
    Join Date
    Apr 2007
    Posts
    33

    Re: Bagels(fermi,pico,bagels) code help

    thank you so much for the help pete. And to everyone else Sorry about the code tags not trying to get on anyone's bad side i'll read that explanation on how to do it again do you put these on java creator or on the thread box?

  9. #24
    Join Date
    Apr 2007
    Posts
    33

    Re: Bagels(fermi,pico,bagels) code help

    i apologize for not having followed your directions but i guess i'm kinda slow . The game just keeps on looping instead of printing out my would you like to play again message when you win or get bagels. Any help you guys give me is greatly appreciated i do understand you guys are not obligated to help me .
    Last edited by Strobe; April 9th, 2007 at 12:20 PM.

  10. #25
    Join Date
    Apr 2007
    Posts
    33

    Re: Bagels(fermi,pico,bagels) code help

    I got the code tags right.

  11. #26
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Bagels(fermi,pico,bagels) code help

    Code Tags: There is a 'Code' button on the editor toolbar. Highlight your code and click on the button. Then when you preview your post you will see it appears in a grey window in a monospaced font.

    You can also add the tags by hand by adding [ Code] ..... [ /Code] around the code (do not include the space after the [, it's only there so that the editor will ignore the tag).

  12. #27
    Join Date
    Apr 2007
    Posts
    33

    Re: Bagels(fermi,pico,bagels) code help

    Code:
     
     public void playGame() 
     {
        Scanner input = new Scanner(System.in);
        System.out.print("Want to play?  " + "Type Y for Yes or N for No and press [Enter]: ") ;
        String answer = input.next();
       while(answer.equalsIgnoreCase("Y") || endGame == false)
       {
       getGuess();
       
       evalGuess(); 
          
       gameStatus() ;
       }
      
       
      if(endGame == true)
       System.out.println("guess =  " +playGuess+  ","+"tries = " +  guessCnt + "  Do you want to play again? Y/N and press [Enter] ") ;
       
     
       
     }
     
     private void quitGame()
     {
       endGame = true ;
       
     }

    Can anyone tell me why this just continues looping even if the winning guess is entered. In the evaluate guess method(evalGuess method) i set it up so that if the winning guess is entered or no digits equal(bagels) my boolean expression endGame = true.

  13. #28
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Bagels(fermi,pico,bagels) code help

    Code:
       while(answer.equalsIgnoreCase("Y") || endGame == false)
    The problem is that 'answer' is set to "Y" to start the game and it is never changed so every time the while statement evaluates answer.equalsIgnoreCase("Y") it returns true and the while loop runs again.

    You should only test 'answer' in the while loop statement if it can be changed in the loop. Otherwise test it's value before the while loop.

  14. #29
    Join Date
    Apr 2007
    Posts
    33

    Re: Bagels(fermi,pico,bagels) code help

    thank you for your help i'm gonna take what you told me and try to fix my code.Again than you very much

  15. #30
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Bagels(fermi,pico,bagels) code help

    Quote Originally Posted by TheCPUWizard
    ...So "tons" of code was written this way. The "first generation" of "C" developers passed it on, and it remains with us today, even though modern compilers [and vastly increased computer capabilities] render such points moot
    I agree that this history is the origin of the phenomenon, but I think that there are enough independent minds appearing in the coding field every year who know that there's negligible practical difference between 'for' or other loop structures these days, that usage would have changed if the alternatives had any other advantage or appeal. Instead, they've pretty well died out. Novices are taught the 'for' loop first because it is essential and can do it all - and once they get familiar with 'for', they soon realise they simply don't need to bother with the others. I think it's an example of evolution in action - the more elegant, descriptive, and specialized (but redundant) loop tools have proven less fit in the coding arena than the cruder, all-purpose functionality of the 'for' loop. Initially it was due to a bad rep, but later just plain human factors.

    OK, it's all (melodramatic) speculation, but I like to think there are these kinds of patterns in the development of software tools and languages

    Imagination is more important than knowledge.
    A. Einstein
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured