Hi every
I am doing over my programming final project trying to see where I have gone wrong. And slowly am beginning to see some of my mistakes, I guess I was a bit overwhelmed anyway, the is part is where I was and still a bit confused.
What is the four questions that have to be asked in order for the players to input the numbers from the smallest to the largest. As you can see this is the first part of the program that need to be builds. This is just round 1. The numbers have to be generated randomly and repeatedly in order for the players to answer and in case the player answers incorrectly, the game should automatically go to the second player. What can I do to get this to happen?
This is round 1
This round consists of four questions, questions 1 through 4. For each question in this round, four (4) random numbers, between 1 to 15, must be generated. The player is then required to re-arrange or enter the numbers from smallest to largest based on the four (4) generated numbers.
here is my codes
Scanner keyin = new Scanner (System.in);
Random randobj = new Random();
int user1age, user2age,count = 0;
String user1name, user2name;
int ans = 0, scores = 0;
System.out.println("Hi Welcome Students!!!");
System.out.println("To the school of counting numbers!!!!");
System.out.println("This school will teach you how to count numbers");
System.out.println("I am going to implement a game of counting but will need two players");
System.out.println("Is there anyone who is interested");
System.out.println("The rules of the game goes like this");
System.out.println("The players must between the ages of 5 and 10");
System.out.println("They must not be older or younger, the comprise of three attempt");
System.out.println("Each players is given three chances to get the correct answer and if not the game goes to the second players");
System.out.println("Do you students think that you are up for the challenge");
System.out.println("well lets have some fun");
System.out.println("Here we go then");
System.out.println("*******************************************************************");
System.out.println("hi honey what is your name");
user1name = keyin.next();
System.out.println("hi honey what is your age");
user1age = keyin.nextInt();
while(user1age < 5 || user1age > 10)
{
if(user1age < 5 || user1age > 10)
{
System.out.println("dan d police!!!! she to young to play this game");
}
else
{
System.out.println("you can play the game");
count++;
}
System.out.println("hi sweetie tell me what is your name");
user2name = keyin.next();
System.out.println("hi sweetie tell me what is your age");
user2age = keyin.nextInt();
while(user2age < 5 || user2age > 10)
{
if(user2age > 5 || user2age > 10)
{
System.out.println("dan you are too old for this game");
}
else
{
System.out.println("you can play this game");
count++;
}
while(user1age < 5 || user1age > 10 && user2age > 5 || user2age > 10)
{
System.out.println("what is user1age");
user1age = keyin.nextInt();
System.out.println("what is user2age");
user2age = keyin.nextInt();
System.out.println("neither of the two of you can play this game");
break;
}
while(user1age > 5 || user1age < 10 && user2age > 5 || user2age < 10)
{
System.out.println("what is user1age");
user1age = keyin.nextInt();
System.out.println("what is user2age");
user2age = keyin.nextInt();
System.out.println("welcome both players to the game");
break;
}
System.out.println("**************welcome to round 1*********");
System.out.println("*******user1 you are up first************");
System.out.println("user1 you are required to re-enter the numbers from the smallest to larges based on the four (4) generated numbers ");
}
for(int i =1; i<5; i++)
{
System.out.println("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*15));
}
System.out.println("enter the smallest number");
int num1 = keyin.nextInt();
if(num1 == ans)
{
System.out.println("yuh gt gyal, congrat!!");
int numtries = 1;
scores = 10;
count++;
}
else
{
System.out.println("the answer is incorrect, try again");
int numtries=1;
scores -=5;
}
System.out.println("enter the next subsequent number");
int num2 = keyin.nextInt(); if(num2 == ans)
if(num2 == ans) - for some reason the compiler is not reading this condition at all, it skit it
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
}
else
{
System.out.println("incorrect, \n try again");
count++;
scores -= 5;
}
System.out.println("enter the next subsequent number");
int num3 = keyin.nextInt();
if(num3 == ans)- the same for here, it skip here and read the last condition even when you entered the correct answere
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
}
else
{
System.out.println("incorrect, \n try again");
count++;
scores -= 5;
}
System.out.println("enter the largest number");
int num4 = keyin.nextInt();
if(num4 == ans)
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
count++;
}
else
{
System.out.println("incorrect, \n try again");
int numtries = 1;
count++;
scores -= 5;
}
}
}
}
here is my output. I am going to show you where i am recieving an output that I dont understand
run:
Hi Welcome Students!!!
To the school of counting numbers!!!!
This school will teach you how to count numbers
I am going to implement a game of counting but will need two players
Is there anyone who is interested
The rules of the game goes like this
The players must between the ages of 5 and 10
They must not be older or younger, the comprise of three attempt
Each players is given three chances to get the correct answer and if not the game goes to the second players
Do you students think that you are up for the challenge
well lets have some fun
Here we go then
*******************************************************************
hi honey what is your name
ashleigh
hi honey what is your age
4
dan d police!!!! she to young to play this game
hi sweetie tell me what is your name
leah
hi sweetie tell me what is your age
13
dan you are too old for this game
what is user1age
3
what is user2age
16
neither of the two of you can play this game
what is user1age
7
what is user2age
8
welcome both players to the game
**************welcome to round 1*********
*******user1 you are up first************
user1 you are required to re-enter the numbers from the smallest to larges based on the four (4) generated numbers
Random Number [2] : 4
Random Number [3] : 12
Random Number [4] : 12
Random Number [5] : 13
enter the smallest number
4
the answer is incorrect, try again - this is the else part of the condition it was supposed to read the first condition if the answser read in correct and if not then it read the else
enter the next subsequent number - this is the second condition which the compiler didnt read
12
enter the next subsequent number- it is the same as abover
12
incorrect, - here it read the else condition not the first
try again
enter the largest number
13
incorrect, -again it read the else condition
try again
BUILD SUCCESSFUL (total time: 1 minute 2 seconds)
and another this is; if numbers are generated from: 0, 2,5,6, and you entered 0 as the smallest, the compiler reads it as correct
but when it generated numbers from: 1, 5, 8,6 and u entered 1 as the smallest go right up to the largest
the compiler read it as incorrect. why is that.
divinity02 6 Posting Whiz in Training
stultuske 1,116 Posting Maven Featured Poster
can you please edit your post. make a separation between your code, your analysis and your actual question. it's pretty hard to make anything out.
Your question itself should be short and to the point, so we'll quickly know what to look for, the rest, which are basically tools for us to go through and check out, should not be in one massive code block.
divinity02 6 Posting Whiz in Training
Hi everyone
I am doing over my programming final project trying to see where I have gone wrong. And slowly am beginning to see some of my mistakes, I guess I was a bit overwhelmed anyway, this is the part is where I was and still a bit confused.
What is the four questions that have to be asked in order for the players to input the numbers from the smallest to the largest.
This is the instructions for round 1
This round consists of four questions, questions 1 through 4. For each question in this round, four (4) random numbers, between 1 to 15, must be generated. The player is then required to re-arrange or enter the numbers from smallest to largest based on the four (4) generated numbers.
What can I do to get the above to happen?
here is my codes
}
for(int i =1; i<5; i++)
{
System.out.println("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*15));
}
System.out.println("enter the smallest number");
System.out.println("yuh gt gyal, congrat!!"); - if four numbers is generated and zero is one of the numbers, you entered it and it read the first statement but when another number is entered it read the else statement even though it is the smallest number
System.out.println("enter the next subsequent number");
int num2 = keyin.nextInt(); if(num2 == ans)
if(num2 == ans) - for some reason the compiler is not reading this condition at all, it skip it
else
{
System.out.println("incorrect, \n try again");
}
System.out.println("enter the next subsequent number");
int num3 = keyin.nextInt(); -
if(num3 == ans)- when the correct answer is entered it read the else statement instead of the first statement
{
System.out.println("incorrect, \n try again");
}
System.out.println("enter the largest number");
int num4 = keyin.nextInt();
and another this is; if numbers are generated from: 0, 2,5,6, and you entered 0 as the smallest, the compiler reads it as correct
but when it generated numbers from: 1, 5, 8,6 and u entered 1 as the smallest go right up to the largest
the compiler read it as incorrect. why is that.
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
I think you misunderstood what stultuske was saying. What he means is that then you post to Daniweb,the code section and only the code section should be pasted into the message using the Code button, while the actually text of the message should be entered with the normal editing window.
Just to clarify the issue, Markdown (the format used by Daniweb) automatically puts anything indented by four or more spaces into a code sub-window. So, if the text is indented, it will show up as code, even if that isn't what you intended. So, when adding a section of plain text, always make sure that the text is not indented, but the code is.
So, for the code you posted, it should look like this:
Scanner keyin = new Scanner (System.in);
Random randobj = new Random();
int user1age, user2age,count = 0;
String user1name, user2name;
int ans = 0, scores = 0;
System.out.println("Hi Welcome Students!!!");
System.out.println("To the school of counting numbers!!!!");
System.out.println("This school will teach you how to count numbers");
System.out.println("I am going to implement a game of counting but will need two players");
System.out.println("Is there anyone who is interested");
System.out.println("The rules of the game goes like this");
System.out.println("The players must between the ages of 5 and 10");
System.out.println("They must not be older or younger, the comprise of three attempt");
System.out.println("Each players is given three chances to get the correct answer and if not the game goes to the second players");
System.out.println("Do you students think that you are up for the challenge");
System.out.println("well lets have some fun");
System.out.println("Here we go then");
System.out.println("*******************************************************************");
System.out.println("hi honey what is your name");
user1name = keyin.next();
System.out.println("hi honey what is your age");
user1age = keyin.nextInt();
while(user1age < 5 || user1age > 10)
{
if(user1age < 5 || user1age > 10)
{
System.out.println("dan d police!!!! she to young to play this game");
}
else
{
System.out.println("you can play the game");
count++;
}
System.out.println("hi sweetie tell me what is your name");
user2name = keyin.next();
System.out.println("hi sweetie tell me what is your age");
user2age = keyin.nextInt();
while(user2age < 5 || user2age > 10)
{
if(user2age > 5 || user2age > 10)
{
System.out.println("dan you are too old for this game");
}
else
{
System.out.println("you can play this game");
count++;
}
while(user1age < 5 || user1age > 10 && user2age > 5 || user2age > 10)
{
System.out.println("what is user1age");
user1age = keyin.nextInt();
System.out.println("what is user2age");
user2age = keyin.nextInt();
System.out.println("neither of the two of you can play this game");
break;
}
while(user1age > 5 || user1age < 10 && user2age > 5 || user2age < 10)
{
System.out.println("what is user1age");
user1age = keyin.nextInt();
System.out.println("what is user2age");
user2age = keyin.nextInt();
System.out.println("welcome both players to the game");
break;
}
System.out.println("**************welcome to round 1*********");
System.out.println("*******user1 you are up first************");
System.out.println("user1 you are required to re-enter the numbers from the smallest to larges based on the four (4) generated numbers ");
}
for(int i =1; i<5; i++)
{
System.out.println("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*15));
}
System.out.println("enter the smallest number");
int num1 = keyin.nextInt();
if(num1 == ans)
{
System.out.println("yuh gt gyal, congrat!!");
int numtries = 1;
scores = 10;
count++;
}
else
{
System.out.println("the answer is incorrect, try again");
int numtries=1;
scores -=5;
}
System.out.println("enter the next subsequent number");
int num2 = keyin.nextInt(); if(num2 == ans)
if(num2 == ans) - for some reason the compiler is not reading this condition at all, it skit it
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
}
else
{
System.out.println("incorrect, \n try again");
count++;
scores -= 5;
}
System.out.println("enter the next subsequent number");
int num3 = keyin.nextInt();
if(num3 == ans)- the same for here, it skip here and read the last condition even when you entered the correct answere
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
}
else
{
System.out.println("incorrect, \n try again");
count++;
scores -= 5;
}
System.out.println("enter the largest number");
int num4 = keyin.nextInt();
if(num4 == ans)
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
count++;
}
else
{
System.out.println("incorrect, \n try again");
int numtries = 1;
count++;
scores -= 5;
}
}
}
}
Note that you only gave us part of the code, and not even a complete part; we would need to see the entire function, or even the entire class, or else just that part of the code actually having problems. You would also need to give specific details about the problem it is giving.
Edited by Schol-R-LEA
divinity02 6 Posting Whiz in Training
okay will do when i reach home, this computer here in work is freezing like hell. sorry about that I thought he meant he wasnt understanding what i had written.
divinity02 6 Posting Whiz in Training
Hi it is me
I am doing over my final programming project to see and understand where I went wrong
This round consists of four questions, questions 1 through 4. For each question in this round, four (4) random numbers, between 1 to 15, must be generated. The player is then required to re-arrange or enter the numbers from smallest to largest based on the four (4) generated numbers.
How am I supposed to accomplish this. How can you create an arrays that could store arrays so that it can compare random numbers
How can I get the question to loop at four times until the players get it either wrong or right.
for(int i =1; i<5; i++)
{
System.out.println("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*15));
}
System.out.println("enter the smallest number");
int num1 = keyin.nextInt();
if(num1 == ans)
{
System.out.println("yuh gt gyal, congrat!!");
int numtries = 1;
scores = 10;
count++;
}
else
{
System.out.println("the answer is incorrect, try again");
int numtries=1;
scores -=5;
}
System.out.println("enter the next subsequent number");
int num2 = keyin.nextInt(); if(num2 == ans)
if(num2 == ans) -
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
}
else
{
System.out.println("incorrect, \n try again");
count++;
scores -= 5;
}
System.out.println("enter the next subsequent number");
int num3 = keyin.nextInt();
if(num3 == ans)
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
}
else
{
System.out.println("incorrect, \n try again");
count++;
scores -= 5;
}
System.out.println("enter the largest number");
int num4 = keyin.nextInt();
if(num4 == ans)
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
count++;
}
else
{
System.out.println("incorrect, \n try again");
int numtries = 1;
count++;
scores -= 5;
}
}
}
}
if(num2 == ans) - the compiler is not reading this condition and I would like to know why and what is wrong with the code
if(num3 == ans) - it is the same here as above
when four number is generated like this: 0,5,3,4, and u enter 0 as the smallest, then 3, 4 and 5 as the largest, it read the zero as correct and the rest as incorrect can u tell me why
stultuske 1,116 Posting Maven Featured Poster
what do you mean "is not reading this condition" ?
why didn't you implement a loop instead of having all those if-else blocks?
Did you debug? did you check the value for num3 and ans?
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
How am I supposed to accomplish this. How can you create an arrays that could store arrays so that it can compare random numbers
If I am reading this correctly, what you are looking to do is have two arrays - one holding the random numbers, the other holding the user input - and compare the results from each, correct? Well, the first step would be to declare an array (obviously), and populate it with the random values:
final int MAX_SIZE = 5;
int[] rand_array = int[ARRAY_SIZE];
for(int i = 0; i < ARRAY_SIZE; i++) {
rand_array[i] = (int)Math.random() % 16;
}
Note that the first element of an array is element zero - an array of 5 elements would go from rand_array[0]
through rand_array[4]
. Thus, you should be initializing i
to zero, as I have above.
Also, I am pretty sure you meant to use modulo 16, not multiply by 15. Using modulo this way will limit the range of the random numbers to 0..15. It isn't the best way to do it - it tends to bias the values to the lower end of the range - but it should be adequate.
A better solution would be to use a Random
object instead:
// declare this before the for() statement
Random randomizer = new Random();
for(int i = 0; i < ARRAY_SIZE; i++) {
rand_array[i] = randomizer.nextInt(MAX_RANGE);
}
Where MAX_RANGE
is the maximum value in the range (15, in this case). You will need to import java.util.Random
at the start of class, however.
Next you would declare a array for the user input, let's say:
int[] user_values = int[ARRAY_SIZE];
Then you would loop through the results, comparing the user input to the random values like so:
if (rand_array[i] == user_values[i]) {
// ... and so forth
Does this help any?
Edited by Schol-R-LEA
divinity02 6 Posting Whiz in Training
hi Schol-R-LEA
rand_array[i] = randomizer.nextInt(max_range);
why am I getting ah set of syntax error underneath this line
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
Can you post the error messages you are getting, please?
divinity02 6 Posting Whiz in Training
I am getting a syntax error here and
why is the compile not reading this condition
for some reason the compiler is not reading this code at all
int array_size =0;
for( i = 0; i<array_size; i++)
rand_array[i] = randomizer.nextInt(max_range);
System.out.println("user1 can you enter the second smallest number");
System.out.println("enter the next subsequent number");
int num2 = keyin.nextInt(); if(num2 == ans)
if(num2 == ans)
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
}
else
{
System.out.println("incorrect, \n try again");
count++;
scores -= 5;
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
We still would need to know what the syntax error is, not just where it is occurring.
As for the second part, you are setting array_size
to zero here:
int array_size =0;
Then you are comparing i < array_size
. If i
is set to zero, it isn't less than zero, is it?
You want to set array_size
to the total size of the array. So, if the array is meant to have five elements, you would set
int array_size = 5;
That's the size of the array, not the index of it. The whle idea is that you are counting up to 4 (which is the highest integer below 5, naturally), so array_size
indicates the endpoint of the loop iteration. Mind you, you want to declare array_size
as final
(that is to say, constant), and use it when you construct the array:
final int array_size = 5;
int[] rand_array = new int[array_size];
This gives you a fixed, named value for the array size, so that if you ever need to change it, you only need to change it in one place.
divinity02 6 Posting Whiz in Training
hi Schol-R-Lea
I am now getting a error message underneath this
rand_array[i] = randomizer.nextInt(max_range);
it is telling to create a field randomizer in countingame and I am also getting an error message under the max_range what can i do to fix this
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
sigh I need you to paste the exact error message into a post so we can see it.
divinity02 6 Posting Whiz in Training
I ask this question before and I dont remember seeing and explanation but here I am asking again
for some reason the compiler is not reading anyone condition in this if statement and I would like to understand why so that i can move on and how do i make the questions repeated in order for the players to keep answering until they got it either wrong/right. here is the block of code am speaking about
if(num2 == ans)
{
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
}
else
{
System.out.println("incorrect, \n try again");
count++;
scores -= 5;
}
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
OK, first off, you are never setting the value of ans
, so ans
is by default zero. Any answer other than 0 will fail.
As for why it never gets to the esle
part, the answer is simple: you have dupicated the if()
condition one successive lines. Correctly indented, it looks like this:
if(num2 == ans)
if(num2 == ans) {
System.out.println("answer correct, excellent!!!");
int numtries = 1;
scores = 10;
}
else {
System.out.println("incorrect, \n try again");
count++;
scores -= 5;
}
The effect of this is that the first if()
always is false, so the second if()
- which is the one with the else
clause - is never executed.
This would have been clear if you had indented the code in a consistent fashion. While which indent style you apply is your choice, consistency in indenting isn't optional. You might want to see if the editor or IDE your using has an auto-indent function, and see if that helps. BTW, just out of curiosity, which editor are you using? Someone here may know if it has such a feature, if you aren't sure yourself.
I would add that you're going about this the wrong way - it is clear you have misunderstood the assignment. The goal isn't to have the user guess the numbers, but to put them in order. What you need to do is show them the numbers in the order they were generated, then ask them to repeat them in order from lowest to highest.
You will want to define three arrays: one for the random numbers in their original order, one for the random numbers in sorted order, and one for the user input.
You will have to write a static
method to sort the numbers, which you will pass the first array to, and which will return the second array.
In main()
, you will have to generate the random numbers in one loop first, then after that loop, you will have to sort the numbers, and then have two loops, one inside the other, to ask the user for the numbers in order.
And to repeat myself yet again: if you are getting a compiler error, please copy the error message and paste it into your post so we can see what it says. Just saying 'it is giving an error' doesn't tell us anything useful.
Edited by Schol-R-LEA
divinity02 6 Posting Whiz in Training
hi schol-r-lea
one question how do i write this static method with getting an error
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
Again, I would need to see both the function you have written, and the error message you are getting, before I could have any hope of answering that question. Sorry to harp on this again and again, but you need to give us all the information needed to understand what is going on with your program.
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
divinity02 has started 38 threads in the Java forum over the last seven months and is still unwilling to ask sensible questions or post proper error messages. None of those threads has ever achieved "solved" status. Maybe we are all wasting our time here.
divinity02 6 Posting Whiz in Training
sorry j didnt mean to
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.