Results 1 to 11 of 11
  1. #1
    mistah is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default guess number game

    helllo (sorry for my bad english i'm learning still)
    we are supposed to make this program from my java txtbook and i am sort of a little being confused..
    we should be making a program that let user input a number between 1 and 20 and the code should be determine if it is too high or too low and also if the user guess right

    we supposed to make with loops but im a little confused
    can you help me? what would the code look like? my teacher not very good..
    sorry if you cant understand

    ask if you have all questions pleas

  2. #2
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    I don't know which method you're using to input the number, so I'll simply call it "inputNumber()". I also don't know which method you're using to create a random number, so I'll simply call that randNum(). Substitute your method where those go. The following goes inside the main routine:

    Java Code:
    int userGuess=0;
    int compNum;
    compNum = inputNumber();
    while(userGuess !=compNum) {
        userGuess = inputNumber();
        if(userGuess >compNumber)
            System.out.println("Guess lower");
        if(userGuess<compNumber)
            System.out.println("Guess higher");
    }
    
    System.out.println("Correct!!!");

  3. #3
    mistah is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    What is difference with compNum and compNumber?
    could something like this be working?
    Java Code:
    import java.util.Scanner;
    
    public class GuessNum {
      public static void main (String[] args) {
    int userGuess=0;
    int compNum;
    compNum = inputNumber();
    while(userGuess !=compNum) {
        userGuess = inputNumber();
        if(userGuess >compNumber)
            System.out.println("Guess lower");
        if(userGuess<compNumber)
            System.out.println("Guess higher");
       }
    
    System.out.println("Correct!!!");
      }
    }

  4. #4
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by mistah View Post
    What is difference with compNum and compNumber?
    could something like this be working?
    Java Code:
    import java.util.Scanner;
    
    public class GuessNum {
      public static void main (String[] args) {
    int userGuess=0;
    int compNum;
    compNum = inputNumber();
    while(userGuess !=compNum) {
        userGuess = inputNumber();
        if(userGuess >compNumber)
            System.out.println("Guess lower");
        if(userGuess<compNumber)
            System.out.println("Guess higher");
       }
    
    System.out.println("Correct!!!");
      }
    }
    That was a typo on my part. Make all of them compNum (or whatever variable you choose). However, there is no such method called inputNumber() or randNumber(). You have to substitute those with the code to do those things. Also, change the line "compNum = inputNumber();" to "compNum = randNumber();" (probably a typo on my part)

  5. #5
    mistah is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    you mean something like..

    Java Code:
    compNum = input.nextInt();
    ?

  6. #6
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by mistah View Post
    you mean something like..

    Java Code:
    compNum = input.nextInt();
    ?
    That would work

  7. #7
    mistah is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    im getting like this picture
    i37.tinypic.com/28w1c40.jpg

    whats problem with it?
    Java Code:
    import java.util.Scanner;
    
    public class GuessingGame {
      public static void main (String[] args) {
    int userGuess=0;
    int compNum;
    Scanner input = new Scanner(System.in);
    
    
    while (userGuess = compNum) {
        userGuess = input.nextInt();
        
        if(userGuess >compNum)
            System.out.println("Guess lower");
        if(userGuess<compNum)
            System.out.println("Guess higher");
       }
    
    System.out.println("Correct!!!");
      }
    }

  8. #8
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    You never picked a random number for compNum...

  9. #9
    mistah is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    how do i do that

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

    Default Some suggestions

    First off, your program won't compile as it is.
    Java Code:
    while (userGuess = compNum)
    The above is an assignment, not a comparation. What I think you want is something like:
    Java Code:
    while (userGuess != compNum)
    ... because you want the cycle to go on while the user hasn't guessed the computer generated number (matzaboy's code had this part right).

    Now some other things to work on:
    • Read about the random generator:

    Random (Java Platform SE 6)
    • ...specially the nextInt(int) method:

    Random (Java Platform SE 6))

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

  11. #11
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    I'm gonna go eat some lunch, thought this might be handy so you don't have to wait on the instructor:
    Java Code:
    import java.io.*;
    import java.security.SecureRandom;
    
    // Generates some random numbers for testing......
    // Throwaway code for study only, not for use.
    
    public class CyberIntegers
    {
        private static final SecureRandom randomStringGenerator;
    
        //
        static{randomStringGenerator = SecureRandom.getInstance("SHA1PRNG");}
    
        // File to place random ints...
        private static File sampleIntegers;
    
        // File to place random ints...
        private static FileWriter writeableSampleIntegers;
    
        // Constructor taking name of file.
        CyberIntegers(String fileName)
        {
            //
            try
            {
                sampleIntegers = new File(fileName);
            }
            catch(java.io.IOException ioException)
            {
                System.out.println(ioException.getMessage());// Failure, print message and exit.
                return;// Explicit return from Constructor not generally coded......
            }
        }
        // Default Constructor.
        CyberIntegers()
        {
            //
            try
            {
                sampleIntegers = new File("samples.dat");
            }
            catch(java.io.IOException ioException)
            {
                System.out.println(ioException.getMessage());// Failure, print message and exit.
                return;// Explicit return from Constructor not generally coded......
            }
        }
        void setDataFile() throws IOException
        {
             writeableSampleIntegers = new FileWriter(sampleIntegers);//
        }
        //
        void writeDataFile()
        {
            // yep, harcode....
            int counter = 0x0000;//
            final int limit = 0x1000;//
            // Use this rather than "\n" for portability and reliability....
            String lineEnding = System.getProperty("line.separator");
            do
            {
                // Get an integer to write to the file.
                Integer nextSampleInteger = new Integer(randomStringGenerator.nextInt());
    
                // Convert to String as named variable
                String nextSampleAsString = nextSampleInteger();//
    
                // Avoid wasted time, use Buffered Writer
                writeableSampleIntegers.write(nextSampleAsString,0x0000,nextSampleAsString.length);
                // Let books and study explain this.
                writeableSampleIntegers.write(lineEnding,0x0000,lineEnding.length);
            }
            while(++counter < limit);
        }
        void closeDataFile() throws IOException
        {
            // Sometimes this needs a flush, sometimes java files will not have a flush.
            // Use a Buffered Writer, probaly has the flush built-in on the close() method....
            writeableSampleIntegers.close();//
        }
        //
        public static void main(String[] args)
        {
            try
            {
                CyberIntegers cypress;//
                if(args != null)
                {
                    if(args.length > 0 )
                    {
                        if(args[0].endsWith(".dat"))
                        {
                            cypress = new CyberIntegers(args[0]);//
                        }
                        else
                        {
                            cypress = new CyberIntegers(args[0].append(".dat"));//
                        }
                    }
                    else
                    {
                        cypress = new CyberIntegers("sammples.dat");//
    
                    }
                    cypress.setDataFile();
                    cypress.writeDataFile();
                    cypress.closeDataFile();
                    return;// Done,......
                }
                else
                {
                    System.out.println("rtfm");
                    return;// Explicit return from main not generally coded......
                }
            }
            catch(Throwable t)
            {
                return;
            }
        }
    }
    Generates 1,000 sample integers for your program to read in, there are plenty of people to explain how to use it.

    I get about half of my utility code writing handy-dandy code-candy for beginners. The rest of it results from months, sometimes years wasted in thinking,....

    ( joke for another thread ... )
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. Replies: 1
    Last Post: 11-03-2008, 02:38 PM
  3. sample of guess high and low game
    By pouria62 in forum AWT / Swing
    Replies: 1
    Last Post: 10-26-2008, 12:57 PM
  4. game
    By amith in forum AWT / Swing
    Replies: 0
    Last Post: 05-19-2008, 05:16 PM
  5. Need help with random game!
    By silverq_82 in forum New To Java
    Replies: 4
    Last Post: 08-07-2007, 02:58 PM

Posting Permissions

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