hi everyone

i am building this java command line game. it is called counting numbers game. here is the detail descripbing of the game
This game consists of 10 questions which are broken down into 3 game rounds. See below for rules regarding the game rounds:

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.

For example and any given question, if the four (4) random generated numbers were 9, 6, 2,13 then the user would have to enter 2, 6 ,9 and 13 to correctly answer this question.

Round 2
Like Round 1, this round consists of four (4) questions, however, questions 5 through 8. For each question in this round, four (4) random numbers, between 1 to 30, 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.

For example and any given question, if the four random generated numbers were 30, 2, 19,7 then the user would have to enter 2, 7 ,19 and 30 to correctly answer this question.
Round 3
This round consists of two (2) questions, question 9 and 10. For each question in this round, 4 random numbers, between 1 to 30 must be generated. Unlike the questions in the previous rounds, the player is then required to re-arrange or enter the number from largest to smallest based on the 4 generated numbers.
For example and any given question, if the four random generated numbers were 26, 1, 30,17 then the user would have to enter 30, 26 ,17 and 1 to correctly answer this question.

This is the expected output of this game
Assume Josanne is player one and Marrissa is player two, see below an example of how the program should function and operate:
Player 1, Question 1 (Round 1): Josanne, re-arrange the following numbers 2, 8,13,7 from smallest to largest
Enter the smallest number: 2
Enter the next subsequent number: 7
Enter the next subsequent number: 8
Enter the largest number: 13
Josanne, you correctly arranged the generated numbers from smallest to largest for question 1.
You earn 50 points and you progress to the next question.
Marrissa you are up next!
Player 2, Question 1 (Round 1): Marrissa, re-arrange the following numbers 12, 15, 5, 2 from smallest to largest
Enter the smallest number: 2
Enter the next subsequent number: 5
Enter the next subsequent number: 15
Enter the largest number: 12
Unfortunately, Marrissa you incorrectly arranged the generated numbers from smallest to largest for question 1, thus you lose 10 points and you do not progress to the next question.
Josanne you are up next!

the thing is when i give it a trial run, I am not getting the int random = new random() to work. it is supposed to generate 4 numbers as you can see in the details decription.

it is looping also when it hit the smallest and largest line and also when hit it the line that say, lets try this one more time. I have only three weeks to hand this program and I really need some help. and can u take a look at the rest of my codes to see where I have gone wrong and how do i get the program to work program. how do i implement the other questions here is a my codes and the output

ublic static void main(String[] args) {
            Scanner sentin = new Scanner (System.in);// scanner object is to get user input
            Random gen = new Random(); // to generate random numbers
            //stsart main

            int randnum;


            String p1,p2, welcomemsg, rulesofgame;
            int age, age2, numtries = 0;
            int scores = 0;//this is used in order to keep track of scores
            int largest = 0, smallest = 0, value = 0, totcount = 0;
            int playagain = 3, keyin = 0; // this is used to keep track on how many they need to play again
            int ans; //this is used to place all answers
            int levcomp;
            int i = 0;// is used to keep track of the failed attempt
            int count = 0;//used to keep track of the count
            int score = 0;

            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("let's go!!!");
            System.out.println("           ");


            System.out.println("player1 what is your name?"); // prompt for the player 1 name
            p1 = sentin.next(); // reading the student name

            System.out.println("player1 how old are you?"); //prompt for the student age
            age = sentin.nextInt(); // reading the student age

            System.out.println("player2 what is your name?"); //prompt for the player 2 name
            p2 = sentin.next(); // reading the student name

            System.out.println("player2 how old are you?"); // prompt for the student age
            age2 = sentin.nextInt(); // reading the student age

            while (age > 5 || age < 11) // checking to see if the student is between the required age group

            {// the loop is about to begin here

          if(age <=5)
            { // start the if
                System.out.println("\n eeeeek %s %s wrong age" +"\n" + "That is not the correct age: read the rules of game " + "\n " + "please enter the correct this time so that we can move one");

            }//end of the if

            else if( age > 5 || age <=10)// checking to see the right age entered 
            {
                System.out.println("well okay lets get started on this game");

            }//end of else if

             System.out.println("\n the era we are living in now, leaves one to wonder " + age);


             System.out.println("let's move on to round 1");



             System.out.println("re-arrange these number from smallest to largest");
             int numcount = (int)(Math.random() * 4 + 1);// to generate the random numbers
              count = sentin.nextInt();

                value = sentin.nextInt();
                if(smallest > value)
             {
                 smallest = value;
             }
              else if(largest > value)
             {
                largest = value;
             }
                System.out.println(largest +":" + smallest);

                System.out.println("enter the count value you both want to start with. \n " + " enter the count between 1 to 15 to count fro the smallest to largest");// asking for where they want to star


                while(count > 1 || count < 4)
            {
                 if(count > 1)
            {
                 System.out.println("\n boombomclat man, start over, p1:" + "\n" + "count");
            }
                else
            {
                System.out.println("ah come on nah, I told you from the smallest to largest: \n");
            }
                System.out.println("lets try this one more time " + "\n "+ " re-enter the count you would like to start from " + "\n " +"it must between 1 to 15");
                count = sentin.nextInt();

                while(numtries !=0 && numtries <=15)
            {
                 if(numtries == 3)
            {
                    System.out.println("OMG!!! you hit the bull's-eye, you have%d numtries left, keep it up " + "\n "+ "numtries");
            }
                 else if(numtries == 2)
            {
                  System.out.println("\n hmmmm %s %s okay one wrong, nothing to worry about, you have %d numtries " + "\n" + "left " + "\n " + "numtries");

            }
                 else if(numtries == 1)
            {
                System.out.println("\n come on, you had enough tries, %s %s numtries left " + "\n " + "no more numtries ");

            }
                System.out.println("level 5d can you re-arrange these number from the smallest to largest?" + "i" + count);

                ans = sentin.nextInt();

                if(ans == i + count)
            {
                score = score + 15; // all correct ans shall increase by 15
                System.out.println("\n  great!! that is correct + 15 points:-) " + "\n " + ans);
                i++; // all correct answer will be increased by 1 with the i variable
            }//end if
              else
            {
                if(numtries == 1)
            {
                System.out.println("\n damn!!!! wrong start over " + "\n " + ans);

            }
                else
            {
                System.out.println("\n what is wrong with you students, incorrect " + "\n "+ "give it another shot!!! " + "\n" + ans);

            }//end if

               numtries--;// decrease the number of tries

            }//end if
            }//end while

             if(numtries == 0)
             {
                 System.out.println("that it am done, no more numtries left");

                 levcomp = i -1;// is used to display the current level
             }





            }
    here is the output of it when i run it 

             run:

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
let's go!!!

player1 what is your name?
K'Ionda
player1 how old are you?
9
player2 what is your name?
lorraine
player2 how old are you?
8
well okay lets get started on this game

the era we are living in now, leaves one to wonder 9
let's move on to round 1
re-arrange these number from smallest to largest
1256
1146
0:0
enter the count value you both want to start with.
enter the count between 1 to 15 to count fro the smallest to largest

boombomclat man, start over, p1:
count
lets try this one more time
re-enter the count you would like to start from
it must between 1 to 15
2468
that it am done, no more numtries left

boombomclat man, start over, p1:
count
lets try this one more time
re-enter the count you would like to start from
it must between 1 to 15
3689
that it am done, no more numtries left

boombomclat man, start over, p1:
count
lets try this one more time
re-enter the count you would like to start from
it must between 1 to 15

you see where it keep looping and it supposed to generate random numbers and it is not doing that. can someone please tell me where i have gone and what to

many thanks in advance. have three weeks to hand this in

             }

         }
    }





















        //end class

Recommended Answers

All 5 Replies

Sorry, but that code is completely unreadable. Can you try to split it down into some sensible methods... try to divide it so that no if/else block or loop spans more than about a dozen lines. Then indent it all according to normal Java standards.

okay j
i have redo it, and I hope that it is readable this time.

public static void main(String[] args) {
        Scanner dashin = new Scanner (System.in);
        Random gen = new Random();

        String p1name, p2name;
        int p1age, p2age, scores, count = 0, sum = 0;
        int largest = 0, smallest = 0, levcomp, ans;
        int age = 0, i;

        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("let's go!!!");
        System.out.println("           ");


        System.out.println("hello p1 what is your name lovely");
        p1name = dashin.next();
        System.out.println("now tell love, what is your age");
        p1age = dashin.nextInt();
        if(age > 5 || age <=10)
        {
            System.out.println("okay you can play ");

        }
        else
        {
            System.out.println("otherwise you cannot play");
        }

        System.out.println("hi p2 what is your name lovely");
        p2name = dashin.next();
        System.out.println("hi p2 what is your age");
        p2age = dashin.nextInt();
        if(age > 5 || age <= 10)
        {
            System.out.println("lets play then, you read the rules then");

        }
        else
        {
            System.out.println("you to old to play ");
        }
       for( i=1; i<=4; i++)
       {
           Random generator = new Random(15) + 1; I am getting a red line here, why it is telling me bad operand type for binary operator why

           for(i =1; i<=15; i++)
        }
       }

my questions are: hwhy are am I getting the red line and how do i remove it
how do i get it to loop back in case the players entered the wrong age to ask to re-enter there age, what is the code.

Random generator = new Random(15) + 1;

how could this not result in a compile time error? generator is an instance of the Random class, it is not a numerical value you can add 1 to, nor it is a String you can concatenate.

What exactly is it you are trying to do?

To get random integers you need to create a single instance of Random, then call its nextInt method for each new random int value you need.

Here's the general form pseudo-code for prompting for a value, validating it, and looping until the user gets it right...

boolean inputIsOK = false;
do {
   prompt for input
   get answer
   if (answer is valid) inputIsOK = true;
   else display error message
} while ( ! inputIsOK);

Joke: I can easily help you "badly". Helping you well (good) is another problem! :-) FWIW, JamesCherrill usually gives good advice. Also, read stultuske's comment about Random(N). IE, create one instance of Random and then call the appropriate methods on that instance to get the next random number. If it is a class member variable, then you can use it any time you want. Just create a public getter method to access the next random number. That way, any of your classes can get one as needed.

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.