Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: A little help please =D

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A little help please =D

    So I got this far

    import javax.swing.JOptionPane;

    public class GuessingGame {
    public static void main (String [] args) {

    int num1 = (int) (System.currentTimeMillis()%10);

    String answerString = JOptionPane.showInputDialog(null,"Guess a number between 1 and 10");
    int answer = Integer.parseInt(answerString);

    if (answer > num1)
    JOptionPane.showMessageDialog(null,"Your answer is too high");
    else if (answer < num1)
    JOptionPane.showMessageDialog(null, "Your answer is too low");
    else
    JOptionPane.showMessageDialog(null, "You won!");

    }
    }

    This program I am making is a guessing game. The user needs to guess a number between 1 and 10. If the user guesses too high or too low then try again. How do I make the user try again without starting over? Anyone want to give me hints and I try my best to solve it. I was going to make the last part of the program look like this but I don't know.

    if (answer > num1)
    JOptionPane.showMessageDialog(null,"Your answer is too high");
    String answerString = JOptionPane.showInputDialog(null,"Guess a number between 1 and 10");
    int answer = Integer.parseInt(answerString);

    else if (answer < num1)
    JOptionPane.showMessageDialog(null, "Your answer is too low");
    String answerString = JOptionPane.showInputDialog(null,"Guess a number between 1 and 10");
    int answer = Integer.parseInt(answerString);

    else
    JOptionPane.showMessageDialog(null, "You won!");

    This gave me errors and I don't think this is the right way to do it. NO LOOPS =D

  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: A little help please =D

    In the future, could you please surround your code in CODE tags, such as the ones found in my signature.
    Is there a reason you don't want to use loops? As It will tidy up your code a lot more than if you were to go down the route of ifs.

    By using a loop, it can endlessly re-prompt the user for an answer by sticking to a single true/false condition, whereas if's get messy.
    Also be sure to always surround your if, if-else blocks with braces as It shows the execution path clearer and makes the code easier to read.

    Additionally, look into the Random class from the Java API, specifically nextInt(int n) to generate a random number, as your approach could generate zeros as well.
    Last edited by newbie; October 7th, 2011 at 03:51 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A little help please =D

    The only reason why I don't want to use a loop is because my teacher doesn't want us to use a loop. =(
    I barely joined this website today and I am still getting use to it. When I paste my code in do I put [*Code in here*] in those brackets so everyone knows which is part of the code?

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A little help please =D

    On General note whenever you write Game there is suppose to be a Game thread which is an while loop continuously running until game is over or stopped, in this loop you check condition, take action and forward the game. you probably would like to structure your program like that.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: A little help please =D

    Quote Originally Posted by valenciajv View Post
    The only reason why I don't want to use a loop is because my teacher doesn't want us to use a loop. =(
    I barely joined this website today and I am still getting use to it. When I paste my code in do I put [*Code in here*] in those brackets so everyone knows which is part of the code?
    At the end and beginning of your code, example
     CODE HERE
    I did this, without the spaces
    [ code]
    your code here
    [/code ]
    It's hard to read your code with out that, if you put it in the correct format perhaps I could help.