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 What's wrong with my code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's wrong with my code?

Matheus Passos
Greenhorn

Joined: Nov 11, 2012
Posts: 6
Hello coderanch, im really new here and i need help.
I want to make a program that ask you to type a number, if its correct, it will to print
"congratulations", if its wrong, it will print "wrong, try again".
But my real problem is: how could i try another number without restarting the program?
Im using Eclipse JEE Juno SR1, windows 7 64 bits (if this help) and here is the code:



As you can see theres nothing after the "else" and its result, because i dont know what to put there. And please i dont want an edited code without explanation.

Kind regards
Matheus
Matheus Passos
Greenhorn

Joined: Nov 11, 2012
Posts: 6
Forgot to say, im using Java jre7.
Ankit Dan
Ranch Hand

Joined: Aug 31, 2012
Posts: 47

I think you should use loops.."for" ..."do while"..."while"
Matheus Passos
Greenhorn

Joined: Nov 11, 2012
Posts: 6
Yea but i didnt figured out how and where to use the "while" function,
in my code, 200 would be false and else true right?
Because while executes something when its on true (correct me if im wrong).
Matheus Passos
Greenhorn

Joined: Nov 11, 2012
Posts: 6
Ok i figured out how to use "while" but i still need something to get back to line 17, so i can combine with:
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1610
    
  13

Matheus Passos wrote:Ok i figured out how to use "while" but i still need something to get back to line 17, so i can combine with:

Remember, a while(...){...} loop executes the given block of code (between the {} braces) repeatedly, until the given condition becomes false (or until you break out of the loop some other way). So you need to think about the following:

  • What actions do you need to repeat each time you go around the loop? This will tell you where to put your "while" command and which lines need to be inside the {} braces.
  • What conditions will cause you to exit the loop? This will tell you what conditions to check each time you go around the loop e.g. do you want to stop when the user guesses correctly, or if the user makes 5 bad guesses?
  • How will you exit the loop and where do you apply your check condition? This will allow you to avoid being stuck in the loop forever.

  • In many programs, you know in advance how many times you want to repeat some processing e.g. 10 times, or once for each object in a list etc, so you would often use a "for" loop rather than a "while" loop.


    No more Blub for me, thank you, Vicar.
    Matheus Passos
    Greenhorn

    Joined: Nov 11, 2012
    Posts: 6
    Sorry chris, i was eating, will check it now.
    Matheus Passos
    Greenhorn

    Joined: Nov 11, 2012
    Posts: 6
    Thanks guys im halfway there, i just need to make 200 only say "congratulations" and not "wrong, try again" :P, but i will try this part alone.
    Wesleigh Pieters
    Ranch Hand

    Joined: Sep 04, 2012
    Posts: 81
    you need to create a test and repeat until it is met, with what you have already you are very close.



    what I did is create a local boolean variable and set to false, then run your code while it is false and set it to true if the num == 200
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: What's wrong with my code?
     
    Similar Threads
    IOException
    return type of TreeMap
    Exit this Do-While w/out an int?
    Is >>>= Broken?
    stack