473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

random number generator

53
Ok im trying to make a lil random number game. I have most of the code and comments on how i want the game to work. but i can not get to it work. if you have any answers any help what so ever it woul be greatly appreciated!

Expand|Select|Wrap|Line Numbers
  1. package mooredMod2;
  2.  
  3. import java.util.Scanner;
  4. import java.util.Random;
  5.  
  6. public class GuessingGame {
  7.  
  8.  
  9. public static void main(String[] args) {
  10.  
  11.  
  12.         Scanner scan = new Scanner(System.in);
  13.  
  14.         // declare a boolean variable named more and set it equal to true
  15.          boolean more = true;
  16.  
  17.         Random generator = new Random();
  18.         while (true)
  19.         {
  20.             // declare an integer variable named numGuesses and 
  21.             // set it equal to zero
  22.  
  23.             int numGuesses = 0; 
  24.  
  25.             // declare a boolean name correct and set it equal to false
  26.  
  27.             boolean correct = false; 
  28.  
  29.             // declare an integer variable named theirGuess
  30.  
  31.             int theirGuess; 
  32.  
  33.             // generate a number between one and ten and store it in a 
  34.             // integer variable named value (see p126)
  35.  
  36.             int value; 
  37.              value = generator.nextInt(10) + 1;
  38.              System.out.println ( + value );
  39.  
  40.             // print out the computer's value (for debugging purposes only) - 
  41.             // you would remove this before
  42.             // really running the program  but leave it in when you 
  43.             // turn your project in
  44.  
  45.              System.out.println("Guess a number between 1 and 10");
  46.                 // read their input from the keyboard using the Scanner 
  47.                 // class and store it in the variable theirGuess
  48.  
  49.                 theirGuess = scan.nextInt ();
  50.  
  51.  
  52.             while (value <= 0 )    // a loop from chapter 5 
  53.             {
  54.                 // increment the numGuesses variable
  55.                 numGuesses = value + 1;
  56.  
  57.  
  58.  
  59.                 if(theirGuess<value)
  60.                 {
  61.                     System.out.println("Too low!");
  62.  
  63.                 }
  64.                 else if (theirGuess>value)
  65.                 {
  66.                     System.out.println("Too high!");
  67.  
  68.                 }
  69.  
  70.  
  71.  
  72.                   //print out that they were correct and 
  73.  
  74.  
  75.  
  76.             }            
  77.                     // tell them how many guesses it took
  78.  
  79. // set the variable correct to true
  80.  
  81. // ask them if they want to play again -            // let them know they need to answer true or false
  82.  
  83.  
  84.             // read their input from the keyboard using the 
  85.             // Scanner class and store their answer in the variable named more    
  86.             scan.nextInt ();
  87.         }
  88.     }//end of main method
  89.  
  90.  
  91. }
Thanks
Sep 21 '08 #1
9 4585
Nepomuk
3,112 Expert 2GB
Hi cdm2883!
First of all, please use the [CODE] ... [/code] tags - this is at least the second time I've added them for you. They are there for a good reason, you know?

Next, what part of the code isn't working? You can't expect people to go through all of your code if they don't even know, what they're looking for. We're all willing to help you, but there are limits.

Greetings,
Nepomuk (Moderator)
Sep 21 '08 #2
cdm2883
53
The partofthe code that i am trying to get working now is where it is suppose to print it the number is to high or to low or if it is coorect
Sep 21 '08 #3
Nepomuk
3,112 Expert 2GB
The part of the code that i am trying to get working now is where it is suppose to print it the number is to high or to low or if it is coorect
OK, you should normally also post what it is (or isn't) doing, but I found it this time: Check that loop while(value <= 0) - is the condition ever true? And if so, will it always be true? If not, why isn't it true? (Just in case you didn't know, "a <= b" means "a is smaller than b or a equals b".)

Greetings,
Nepomuk
Sep 21 '08 #4
cdm2883
53
thank you for helping me find that error that i did not catch! now i am trying to figure out how to write the code for when they are correct, and would like to play again. this is whati have so far...

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Would you like to play again? (Eneter true or false)");
  2.  
  3. while (more = true) ;
  4. {
  5. }
I think i need to use the if and else statments but i dont know what code to write if they say true to ask them to enter a number, and if they put false how to terminate the program


Thanks
Sep 21 '08 #5
jx2
228 100+
thank you for helping me find that error that i did not catch! now i am trying to figure out how to write the code for when they are correct, and would like to play again. this is whati have so far...

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Would you like to play again? (Eneter true or false)");
  2.  
  3. while (more = true) ;
  4. {
  5. }
I think i need to use the if and else statments but i dont know what code to write if they say true to ask them to enter a number, and if they put false how to terminate the program


Thanks
i dont knoe anythink about your program but i see big problem in your last post
Expand|Select|Wrap|Line Numbers
  1. while (more = true) ;
  2. {
  3. }
  4. //there are at least two mistakes in one line the  "more=true" is always true
  5. // i think you meant " more==true" <--- notice double "="  
  6. //second the semicolon at the end of first line "{}" will be ignored !!
  7.  
  8. //correct while loop will look something like that:
  9.  
  10. while(more == true){
  11.       //source code here
  12. }
  13.  
Sep 22 '08 #6
Nepomuk
3,112 Expert 2GB
thank you for helping me find that error that i did not catch! now i am trying to figure out how to write the code for when they are correct, and would like to play again. this is whati have so far...

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Would you like to play again? (Eneter true or false)");
  2.  
  3. while (more = true) ;
  4. {
  5. }
I think i need to use the if and else statments but i dont know what code to write if they say true to ask them to enter a number, and if they put false how to terminate the program


Thanks
Actually, that won't even work with the corrections by jx2. What you want will be something like this:
Expand|Select|Wrap|Line Numbers
  1. boolean more;
  2. do {
  3.     more = false;
  4.     //... the code you already have here
  5.     System.out.println("Would you like to play again? (Enter true or false)");
  6.     String answer = ...; // Read the answer
  7.     // If the answer is "true", set the boolean "more" to true, else to false
  8. } while(more)
For reading the answer, have a look at the Scanner class and System.in.

Greetings,
Nepomuk
Sep 22 '08 #7
cdm2883
53
ok so i have added this code...

scan.next();
do {
more = true;
if (theirGuess == value)
{
System.out.println("Would you like to play again? (Eneter true or false)");
}

}while (more);

{
System.out.println("Guess a number between 1 and 10");


but now my program will skip code diffrent parts of it, i dont know how else to explain it. If you tell me what you need to look at i will post it.

Thanks
Sep 23 '08 #8
cdm2883
53
and is that part of the code going to do what i want it to?


Thanks
Sep 23 '08 #9
r035198x
13,262 8TB
ok so i have added this code...

scan.next();
do {
more = true;
if (theirGuess == value)
{
System.out.println("Would you like to play again? (Eneter true or false)");
}

}while (more);

{
System.out.println("Guess a number between 1 and 10");


but now my program will skip code diffrent parts of it, i dont know how else to explain it. If you tell me what you need to look at i will post it.

Thanks
Pay special attention to your datatypes. If you are reading the input as strings then you should use the equals method to compare the strings rather than ==.
Sep 23 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.