Results 1 to 11 of 11
Thread: guess number game
- 11-18-2008, 10:31 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
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, 10:56 PM #2
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
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!!!");
- 11-18-2008, 11:03 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
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!!!"); } }
- 11-18-2008, 11:05 PM #4
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
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, 12:04 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
you mean something like..
?Java Code:compNum = input.nextInt();
- 11-19-2008, 01:27 AM #6
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
- 11-20-2008, 10:09 PM #7
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
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!!!"); } }
- 11-20-2008, 10:16 PM #8
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
You never picked a random number for compNum...
- 11-21-2008, 10:48 PM #9
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
how do i do that
- 11-22-2008, 12:57 AM #10
Some suggestions
First off, your program won't compile as it is.
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).Java Code:while (userGuess != compNum)
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-23-2008, 03:37 AM #11
I'm gonna go eat some lunch, thought this might be handy so you don't have to wait on the instructor:
Generates 1,000 sample integers for your program to read in, there are plenty of people to explain how to use it.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; } } }
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
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
how to extract the number of the image which looks like a number
By Crest.Boy in forum Java ServletReplies: 1Last Post: 11-03-2008, 02:38 PM -
sample of guess high and low game
By pouria62 in forum AWT / SwingReplies: 1Last Post: 10-26-2008, 12:57 PM -
game
By amith in forum AWT / SwingReplies: 0Last Post: 05-19-2008, 05:16 PM -
Need help with random game!
By silverq_82 in forum New To JavaReplies: 4Last Post: 08-07-2007, 02:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks