The Artima Developer Community
Sponsored Link

Java Answers Forum
guessingamehelp

6 replies on 1 page. Most recent reply: Oct 23, 2003 2:21 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
heather

Posts: 1
Nickname: heather
Registered: Oct, 2003

guessingamehelp Posted: Oct 22, 2003 12:22 PM
Reply to this message Reply
Advertisement
the guessing game code i have to write requires the user to guess a random integer between 1 and 50. if the guess is 5 or more from the random number the user doesnt get but if its within 4 or less the user gets it correctly. how do i write this part of the code?


Dar

Posts: 9
Nickname: dar
Registered: Oct, 2003

Re: guessingamehelp Posted: Oct 23, 2003 11:59 AM
Reply to this message Reply
int randomNum = 10; --This is the randomly generated number
boolean winNo = false;

if (userGuess + 5 >= randomNum)
winNo = false;
else
winN0 = true;


variable userGuess holds the user's guess value

Dar

Posts: 9
Nickname: dar
Registered: Oct, 2003

Re: guessingamehelp Posted: Oct 23, 2003 12:09 PM
Reply to this message Reply
I'm sorry, but the response I posted will not give you the desired result.

I'll get back to you.


Sorry

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: guessingamehelp Posted: Oct 23, 2003 12:15 PM
Reply to this message Reply
I think you mean if the user guesses the right number in five or fewer guesses, it is considered a win? Anyhow, post what you have written so far and remember to use the java formatting tags, described in the gray box title "Formatting Your Post" at the right, when you are replying.

Subha

Posts: 16
Nickname: nikki
Registered: Sep, 2003

help urgent Posted: Oct 23, 2003 12:38 PM
Reply to this message Reply
If i have the following code....how can i get the first value from p
      int [] p;       
       p = new int[4];
      Random gen = new Random();
      for(int i=1;i<=3;i++)
      {
    int pos = gen.nextInt(9);       
     //System.out.println(pos);
     p[i] = pos;
      //int q = get(p[i]);//not working
     System.out.println(q); 
     }

Dar

Posts: 9
Nickname: dar
Registered: Oct, 2003

Re: guessingamehelp Posted: Oct 23, 2003 12:55 PM
Reply to this message Reply
Okay! I'm sorry about the code I posted earlier that didn't work. Here's the code that will work for you.

public static void main (String args[]) {

String usernum = "";
String result = "";

int randomNum = 0;
int userNum = 0;

randomNum = 20; //You can randomly generate this.

usernum = JOptionPane.showInputDialog("Enter Your Guess");

userNum = Integer.parseInt(usernum);

if (userNum >= randomNum && userNum < randomNum + 5)
result = "You didn't exceed the generated number by more than 5. You win!!!";

else if (userNum <= randomNum && userNum > randomNum - 4)
result = "You're not more than 4 below the generated number. You win!!!";

else
result = "You're either more than 5 above or more than 4 below the generatd number. You lose!!!";

JOptionPane.showMessageDialog (null, result, "Guessing Game", JOptionPane.INFORMATION_MESSAGE);


System.exit(0);

}//end main

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: guessingamehelp Posted: Oct 23, 2003 2:21 PM
Reply to this message Reply
It might be a little more elegant to use abs().

Flat View: This topic has 6 replies on 1 page
Topic: JSP & Servlets Previous Topic   Next Topic Topic: AWTEvent.getSource()

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use