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! -
package mooredMod2;
-
-
import java.util.Scanner;
-
import java.util.Random;
-
-
public class GuessingGame {
-
-
-
public static void main(String[] args) {
-
-
-
Scanner scan = new Scanner(System.in);
-
-
// declare a boolean variable named more and set it equal to true
-
boolean more = true;
-
-
Random generator = new Random();
-
while (true)
-
{
-
// declare an integer variable named numGuesses and
-
// set it equal to zero
-
-
int numGuesses = 0;
-
-
// declare a boolean name correct and set it equal to false
-
-
boolean correct = false;
-
-
// declare an integer variable named theirGuess
-
-
int theirGuess;
-
-
// generate a number between one and ten and store it in a
-
// integer variable named value (see p126)
-
-
int value;
-
value = generator.nextInt(10) + 1;
-
System.out.println ( + value );
-
-
// print out the computer's value (for debugging purposes only) -
-
// you would remove this before
-
// really running the program but leave it in when you
-
// turn your project in
-
-
System.out.println("Guess a number between 1 and 10");
-
// read their input from the keyboard using the Scanner
-
// class and store it in the variable theirGuess
-
-
theirGuess = scan.nextInt ();
-
-
-
while (value <= 0 ) // a loop from chapter 5
-
{
-
// increment the numGuesses variable
-
numGuesses = value + 1;
-
-
-
-
if(theirGuess<value)
-
{
-
System.out.println("Too low!");
-
-
}
-
else if (theirGuess>value)
-
{
-
System.out.println("Too high!");
-
-
}
-
-
-
-
//print out that they were correct and
-
-
-
-
}
-
// tell them how many guesses it took
-
-
// set the variable correct to true
-
-
// ask them if they want to play again - // let them know they need to answer true or false
-
-
-
// read their input from the keyboard using the
-
// Scanner class and store their answer in the variable named more
-
scan.nextInt ();
-
}
-
}//end of main method
-
-
-
}
Thanks
9 4585
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)
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
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
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... -
System.out.println("Would you like to play again? (Eneter true or false)");
-
-
while (more = true) ;
-
{
-
}
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
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... -
System.out.println("Would you like to play again? (Eneter true or false)");
-
-
while (more = true) ;
-
{
-
}
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 -
while (more = true) ;
-
{
-
}
-
//there are at least two mistakes in one line the "more=true" is always true
-
// i think you meant " more==true" <--- notice double "="
-
//second the semicolon at the end of first line "{}" will be ignored !!
-
-
//correct while loop will look something like that:
-
-
while(more == true){
-
//source code here
-
}
-
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... -
System.out.println("Would you like to play again? (Eneter true or false)");
-
-
while (more = true) ;
-
{
-
}
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: - boolean more;
-
do {
-
more = false;
-
//... the code you already have here
-
System.out.println("Would you like to play again? (Enter true or false)");
-
String answer = ...; // Read the answer
-
// If the answer is "true", set the boolean "more" to true, else to false
-
} while(more)
For reading the answer, have a look at the Scanner class and System.in.
Greetings,
Nepomuk
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
and is that part of the code going to do what i want it to?
Thanks
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 ==.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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++)
{
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |