can someone tell why am i getting loopin. trying to build this game and it is only looping

* @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         String p1name, p2name;
        int p1age, p2age, score, sum = 0;
        int largest = 0, smallest = 0, levcomp, ans = 0;
        int age = 0, i = 0, value, num = 0;
        int  mistakes0;
        int  mtakes = 0;
        int numtries = 0;
        int count = 1;

        // required variables are entered here
        Scanner userin  = new Scanner (System.in);
        Random randomgen = new Random();

         System.out.println("Welcome Students, To the School of Hard Knocks!!!!!");   
         System.out.println("Today we are about to learn to count numbers ");
         System.out.println("I am looking for two players to play a game of counting numbers, who is going to be the two players?");
         System.out.println("K'Ionda, Lorraine, anyone, okay lets take K'Ionda and Lorraine to play ");
         System.out.println("Lets see how good you both are at counting numbers");
         System.out.println("The rules of the game are that both players must between the ages of 5 and 10");
         System.out.println("The rules states that each players have a miinimum of three atempts");
         System.out.println("The game will be forfeited if each players answer the three questions wrong");
         System.out.println("If a player get three question wrong, the other player automatically wins the game");
         System.out.println("The numbers are generated randomly, so the players will input the numbers from smallest to largest");
         System.out.println("Another one also is from the largest to the smallest");
         System.out.println("for every correct answer 15 points are given and for every wrong answer 5 points are deducted");

         System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

         System.out.println("        ");
         System.out.println("So ah guess you guys are ready to have some fun");
         System.out.println("lets give a  hearty round of applause for the two volunteers " + " clap!! clap!!");
         System.out.println(" okay let's go!!!");
         System.out.println("           ");



             do

            {
            //if(age < 5 && age > 10)

             System.out.println("p1 enter your name please");
              p1name = userin.next();

             System.out.println("p1 enter your age please");
             p1age = userin.nextInt();
            }
             while(age < 5 || age > 10);

          count++;
        }

it has to loop in the sense if the player enter the wrong age where they can go back to re enter but it is not doing that. it is looping back to the name and age this is the output

Welcome Students, To the School of Hard Knocks!!!!!
Today we are about to learn to count numbers
I am looking for two players to play a game of counting numbers, who is going to be the two players?
K'Ionda, Lorraine, anyone, okay lets take K'Ionda and Lorraine to play
Lets see how good you both are at counting numbers
The rules of the game are that both players must between the ages of 5 and 10
The rules states that each players have a miinimum of three atempts
The game will be forfeited if each players answer the three questions wrong
If a player get three question wrong, the other player automatically wins the game
The numbers are generated randomly, so the players will input the numbers from smallest to largest
Another one also is from the largest to the smallest
for every correct answer 15 points are given and for every wrong answer 5 points are deducted
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

So ah guess you guys are ready to have some fun
lets give a hearty round of applause for the two volunteers clap!! clap!!
okay let's go!!!

p1 enter your name please
h
p1 enter your age please
4
p1 enter your name please
l
p1 enter your age please
4
p1 enter your name please
l
p1 enter your age please
7
p1 enter your name please
BUILD STOPPED (total time: 24 seconds)

can someone help pla

You aren't setting age to be a new value so it always stays at its original value of zero. Thats always less than 5 so your loop never ends.

do i always to give age a value everytime the age is re-enter

you need to reset the value of age within this loop:

do
            {
            //if(age < 5 && age > 10)
             System.out.println("p1 enter your name please");
              p1name = userin.next();
             System.out.println("p1 enter your age please");
             p1age = userin.nextInt();
            }
             while(age < 5 || age > 10);

To shortly explain why this loops:
this piece of code will loop as long as age is smaller than 5 or bigger than 10.

Let's say you enter that loop and the value for age is 4 ...
you never reset the value of age, so at the end of the iteration, it will still be 4, so the loop restarts. again, the value is not changed, so ...

" i always to give age a value everytime the age is re-enter"

No you don't. You give a new value to p1age, not to age

I have this code, i am hope that it would generate random number but I am getting this error: int cannot be dereferenced, can someon tell me how to fix it

q1 = Math.abs(q.nextInt()%15)+1;

not random number but question for the players to answers but am getting error int cannot be dereferenced. continued from my last post above

can you show your current code and the exact stacktrace?

Probably q is an int variable, not an instance of the Random class

oj james ah will try and remove it and put something else and see

ah remove it and put it as a string and now I am getting an error

q1 = Math.abs(q.nextInt()%15)+1; I am getting an error by nextInt()% 15 +1;

been trying to figure out with is the error there but cant get at it.

hi j

how do you generate repeated random numbers in java. plz dont be mad at me. this is what what i have, will this generated repeated random numbers

Random randongen = new Random();
                System.out.println("generate for random number between 1 and 15");
                int randomint = randomgen.nextInt(15)+1;
                System.out.println("generated: " + randomInt);

                System.out.println(randomint);

Just keep calling randomgen.nextInt. Each time you call it you get a new random number

Random randongen = new Random();
       System.out.println("generate many random numbers between 1 and 15");
       for (int i = 0; i < howManyNumbers; i++) {
                int randomint = randomgen.nextInt(15)+1;
                System.out.println("generated: " + randomInt);
       }

okay j thanks been searching the web all the time, my other questions above wasnt answered i am wondering where is the errror. cant figure it out.

q1 = Math.abs(q.nextInt()%15)+1; I am getting an error by nextInt()% 15 +1;

I already answered that 2 hours ago.

now I am getting an error under the whole thing
q1 = Math.abs(questions.nextInt()15)+1;

You seem to be trying anything in that statement without thinking about what you are doing.
nextInt is an instance method in the Random class. To call it you need an instance of the Random class. Not an int, not a String, not something you found down the back of your sofa.
Just look at the code I posted an hour ago. It shows exactly how to do it.

hi j
now am getting syntax error under the whole blck of code

hey j
i have the same code as you put above, but it is only generating one number at a time, what I want it to do is to generate a set of numbers at the same time, ah mean something like this: 2, 8, 9, 10 where the player can arrange it from the smallest to the largest but it is not doing it. it generated only one number at a time.

Random randomgen = new Random();
                System.out.println("generate for random number between 1 and 15");
                 for(i = 1; i<4; i++)
                 {
                   int randomint = randomgen.nextInt(15)+1;
                    System.out.println("generated: " + randomInt);

                System.out.println("random number [" + (i+1) + "] : " + (int) (Math.random() *15));
                 System.out.println("please enter the smallest number");
                smallest = userin.nextInt();

                System.out.println("please enter the next subsequent number");
                //smallest = userin.nextInt();

                System.out.println("please the next subsequent number");
                //smallest = userin.nextInt();

                System.out.println("please enter the largest namber ");
               // largest = userin.nextInt();

That's how Random works - one number at a time. If you want multiple random numbers then use a loop like I showed, and inside the loop put the numbers into an array or ArrayList.

okay j thanks

thanks j

You're welcome!

one more thing j
i am trying to use this code but it is giving this error int cannot be derenferenced

if(numtries.equalsIgnorecase("3"))
what do i do to get rid of the error or how do i fix it

You didn't show the declaration of numtries, but I suspect it's an int.
equalsIgnoreCase is a method for the String class, so you can't use it for an int.
You can test the value of an int with a simple ==, as in

if (i == 3)

thank again j ah got it, the syntax error is gone ah put it as a string

hi j

can you tell me what is wrong with this block of code
the error that I am getting is: array required but int found

cant say for sure where it is

for(i = 1; i< round; i++)
              {
                  int nums[] = new int[15];
                  p1_num[0]=randomgen.nextInt(15)+1;
                  p1_num[2]=randomgen.nextInt(15)+1;
                  p1_num[3]=randomgen.nextInt(15)+1;
                 System.out.println("p1, ques1" + "rearrange these      numbers from the smallest to largest");
                  System.out.println("p1_num[0 " + "," + "p1_nun[1]" + "," + "p1_num[2]" + "," + "p1_num[3]");

need an ans asap
many thanks

Line 8 you have a missing ] and your quotes are all wrong - there should not be quotes round the array references. It should look more like

(p1_num[0] + ", " + p1_num[1] + ...

thank you james got the loop to stop and it working properly. all ah need now is a while loop for it to loop until the players get the right answer, but mind I am working on it just taking my time and testing it until i got the right loop.

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.