# guess number game

• 11-18-2008, 11:31 PM
mistah
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
• 11-18-2008, 11:56 PM
matzahboy
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:

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!!!");```
• 11-19-2008, 12:03 AM
mistah
What is difference with compNum and compNumber?
could something like this be working?
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!!!");   } }```
• 11-19-2008, 12:05 AM
matzahboy
Quote:

Originally Posted by mistah
What is difference with compNum and compNumber?
could something like this be working?
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)
• 11-19-2008, 01:04 AM
mistah
you mean something like..

Code:

`compNum = input.nextInt();`
?
• 11-19-2008, 02:27 AM
matzahboy
Quote:

Originally Posted by mistah
you mean something like..

Code:

`compNum = input.nextInt();`
?

That would work
• 11-20-2008, 11:09 PM
mistah
im getting like this picture
i37.tinypic.com/28w1c40.jpg

whats problem with it?
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!!!");   } }```
• 11-20-2008, 11:16 PM
matzahboy
You never picked a random number for compNum...
• 11-21-2008, 11:48 PM
mistah
how do i do that
• 11-22-2008, 01:57 AM
CJSLMAN
Some suggestions
First off, your program won't compile as it is.
Code:

`while (userGuess = compNum)`
The above is an assignment, not a comparation. What I think you want is something like:
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:

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

Random (Java Platform SE 6))

Luck,
CJSL
• 11-23-2008, 04:37 AM
Nicholas Jordan
I'm gonna go eat some lunch, thought this might be handy so you don't have to wait on the instructor:
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 ... )