Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
  1. #21
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    no i dont ?
    can you please explain, and maybe suggest a way to fix this?
    how do i make it a guessing game? randomize??

  2. #22
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default use Random

    You can use the Random class:
    Random (Java Platform SE 6)
    along with it's nextInt() method:
    Random (Java Platform SE 6) nextInt)
    and this is a sample how it's used (see my second post):
    Random number generater

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #23
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    i didnt see your post until after i fixed my program, but this is the result.
    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Ch7Ex10 {
    
    public static void giveHint(int compNum, int userNum) {
      //give hint
         if  (userNum > compNum) {
          System.out.println("Try a lower number");
        } else {
          System.out.println("Try a higher number");
        }
    }
    public static void main (String[]args) {
    
      int compNum, userNum;
        Scanner input = new Scanner (System.in);
       
           while(true) {
        //get value from user
        System.out.println("Enter a number between 1 and 20: ");
        userNum = input.nextInt();
        //randomize computer number
        Random r = new Random(10);    
        compNum = r.nextInt(20);
        
        //determines if user wins, else goes to hint
        if (userNum == compNum) {
          System.out.println("You won!");
          break;
        }else {
          giveHint(compNum, userNum);
        }
      }
    }
    }
    if you have any suggestions for anything else, please share.
    if not, then thank you, you both have been helpful!

    oh, but i'd like to know why the random number is always 13?

  4. #24
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Two things....
    • What happens if you change to the following statement:

    Java Code:
    Random r = new Random();
    • Did you read my second post in the link of my previous post ? One of the random numbers will be zero (0). Do you want zero to be one of the randomly generated numbers?

    I don't know why it always generates 13.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #25
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    The random number generation should be outside of the while loop, otherwise the number will change each time he guesses.

  6. #26
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    The random number generation should be outside of the while loop, otherwise the number will change each time he guesses.
    ah, yes... good catch... I wasn't paying attention.
    Java Code:
    Random r = new Random();    
    compNum = r.nextInt(20);
    
    while(true) {...
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. how to give delay?
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-17-2008, 06:57 PM
  3. Please give me a solution
    By ivvgangadhar in forum AWT / Swing
    Replies: 5
    Last Post: 11-14-2008, 02:43 PM
  4. code hint scroll bar ?
    By tryit in forum Eclipse
    Replies: 3
    Last Post: 04-28-2008, 08:07 AM
  5. give me a advise
    By sireesha in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 02:38 AM

Posting Permissions

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