hi everyone

I just want to say merry christmas to each and everyone here. it has been a while, yes ah kno but anyway have been doing this programming school assignment game for over two months
iam having real difficulties getting it to work the way it is supposed to work. I have re-do this game numerous time because of the logical errors that was there and couldnt figure out and still cant.
the game is like this, it has 3 round. it also has 10 questions that need to be generated using the random generator. and it supposed to go from level 1 -10
round 1 - add with 4 levels, ah mean level 1,2,3 and 4, four question. one question for each level.
round 2-subtract with 4 level, ah mean level 5, 6, 7 and 8 four questions also, one for each level
round 3 -divide with 2 level ah mean level 9, and 10 2 question one for each level
on any given level if the user enter a wrong answer, the game supposed to iterate and stay on the said level and repeated the same question until the user get it correct and it goes for all the rounds. rounds 1-3.

i am not getting it to iterate at all. I am using a while loop along with nested if else if. and right now the game is stuck on . here is my codes and the output. any help will be greatly appreciated and this has to be handed up on the 31 december 2015, dont have much time.

  System.out.println("******************welcome to round 1************************");

        System.out.println("Hi what is your name");
        playername = userinput.next();
        System.out.println(playername+" you have a very unique name, my dearest");
        System.out.println(playername+" how old are you");
        playerage = userinput.nextInt();
        //initializing the player name and age

         // this is checking to see wjther the player entered the correct age
        if(playerage < 8 || playerage > 13)
        {
            System.out.println(playername+" you have entered an invalid age");
            System.exit(0);//this indicating that the player has entered the wrong age, so the game exit
        }
        else
        {
            System.out.println(playername+" come let's play");
            //this is statinf that the player entered the correct age
        }


        i=1;
        while(i<=4)
        {
            if(i<=4)
            {
                //get random numbers
                rand  = 1 + randobj.nextInt(25);
                rand2 = 1 + randobj.nextInt(8);
                correctans = rand + rand2;
                incorrectans = 25 + randobj.nextInt(50);

                //inidicating the level and round the user would start with
                System.out.println(playername+" round 1 "+ "level "+ i + " question "+ i + " you are required to match the letter to the corresponding answer");
                System.out.println(playername+" please add "+ rand + " + "+ rand2);

                //get the multiple choice answer
                System.out.println("A:"+correctans);
                System.out.println("B:"+incorrectans);
                choice = userinput.next().charAt(0);

                if(choice == 'A' || choice == 'a')
                {
                    System.out.println(playername+" well done kiddo, correct is right, keep it up");
                    score += score + 650;
                    System.out.println("round 1");
                    System.out.println("level "+ i);
                    totalscore += score;
                    System.out.println("the total score is "+ totalscore);
                    i++;
                    correctans++;//to count all the correct answer entered
                }
                else if(choice == 'B' || choice == 'b')
                {
                    System.out.println(playername+" no troll!! not the correct answer, you need to try again");
                    score -= score - 225;
                    System.out.println("round 1");
                    System.out.println("level "+ i);
                    totalscore -= score;
                    System.out.println("the total score is "+ totalscore);
                    i++;
                    wrongcount++;//to count all the incorrect ans
                }
            }
        }

               i=1;
            while(chances !=0 && i<=4)
            {
                    if(chances < 4)
                     {
                        //get random numbers
                        rand  = 1 + randobj.nextInt(25);
                        rand2 = 1 + randobj.nextInt(8);
                        correctans = rand + rand2;
                        incorrectans = 25 + randobj.nextInt(50);

                        //inidicating the level and round the user would start with
                        System.out.println(playername+" round 1 "+ "level "+ i + " question "+ i + " you are required to match the letter to the corresponding answer");
                        System.out.println(playername+" please add "+ rand + " + "+ rand2);

                        //get the multiple choice answer
                        System.out.println("A:"+correctans);
                        System.out.println("B:"+incorrectans);
                        choice = userinput.next().charAt(0);
                    }
                    if(choice == 'A'|| choice == 'a')
                    {
                        System.out.println(playername+" keep it up, you are doing great, you have %d chances left "+ chances);
                        level--;
                        chances--;
                    }
                    else if(choice == 'B' || choice == 'b')
                    {
                        System.out.println(playername+"\n come on now!!! you have one wrong, you have %d chances lefet "+ chances);
                        System.out.println("round 1");
                        System.out.println("level "+ i);
                        chances--;
                    }

                    else if(chances < 3)
                    {
                        //get random numbers
                        rand  = 1 + randobj.nextInt(25);
                        rand2 = 1 + randobj.nextInt(8);
                        correctans = rand + rand2;
                        incorrectans = 25 + randobj.nextInt(50);

                        //inidicating the level and round the user would start with
                        System.out.println(playername+" round 1 "+ "level "+ i + " question "+ i + " you are required to match the letter to the corresponding answer");
                        System.out.println(playername+" please add "+ rand + " + "+ rand2);

                        //get the multiple choice answer
                        System.out.println("A:"+correctans);
                        System.out.println("B:"+incorrectans);
                        choice = userinput.next().charAt(0);
                    }
                    if(choice == 'A' || choice == 'a')
                    {
                        System.out.println(playername+"well done, it is correct, keep going, you have %d chances left "+ chances);
                        System.out.println("round 1");
                        System.out.println("level "+ i);
                        chances--;
                    }
                    else if(choice == 'B'|| choice == 'b')
                    {
                        System.out.println(playername+"\n oh my that is not correct you have %d chances left "+ chances);
                        System.out.println("round 1");
                        System.out.println("level "+ i);
                        chances--;
                    }

                    else if(chances < 2)
                    {
                        //get random numbers
                        rand  = 1 + randobj.nextInt(25);
                        rand2 = 1 + randobj.nextInt(8);
                        correctans = rand + rand2;
                        incorrectans = 25 + randobj.nextInt(50);

                        //inidicating the level and round the user would start with
                        System.out.println(playername+" round 1 "+ "level "+ i + " question "+ i + " you are required to match the letter to the corresponding answer");
                        System.out.println(playername+" please add "+ rand + " + "+ rand2);

                        //get the multiple choice answer
                        System.out.println("A:"+correctans);
                        System.out.println("B:"+incorrectans);
                        choice = userinput.next().charAt(0);
                     }
                        if(choice == 'A' || choice == 'a')
                        {
                            System.out.println(playername+" no right, that is correct, you have %d chances left, let keep it that way");
                            System.out.println("round 1");
                            System.out.println("level "+ i);
                            chances--;
                        }
                        else if(choice == 'B'|| choice == 'b')
                        {
                            System.out.println(playername+" nah man come on!!! that is incorrect you  have %d chances left "+chances);
                            System.out.println("round 1 ");
                            System.out.println("level "+i);
                            chances--;
                        }

                        else if(chances < 1)
                        {
                            //get random numbers
                            rand  = 1 + randobj.nextInt(25);
                            rand2 = 1 + randobj.nextInt(8);
                            correctans = rand + rand2;
                            incorrectans = 25 + randobj.nextInt(50);

                            //inidicating the level and round the user would start with
                            System.out.println(playername+" round 1 "+ "level "+ i + " question "+ i + " you are required to match the letter to the corresponding answer");
                            System.out.println(playername+" please add "+ rand + " + "+ rand2);

                            //get the multiple choice answer
                            System.out.println("A:"+correctans);
                            System.out.println("B:"+incorrectans);
                            choice = userinput.next().charAt(0);
                        } 
                        if(choice == 'A'|| choice == 'a')
                        {
                            System.out.println(playername+" woot woot nice try, correct you have no %d chances left "+chances);
                            System.out.println("round 1");
                            System.out.println("level "+ i);
                            chances--;
                        }
                        else
                        {
                             System.out.println(playername+" game over");
                        }

this is the output

run:
Welcome to Summer's Eve Multiple Choice Math Program
The game consist of 3 round with 6 level
The age limit of this game is 8 - 13 years 
The player wins the game if all questions are answerred correctly
The player loses the game if they run out of chances
Each player has 4 chances
Each correct answer is rewarded with a maximum of 650 points
Each incorrect answer will lose half of the alloted point given 
A report is submitted at the end of the game
A player only win the game if all 10 question are answered correctly
A player loses the game if the age is entered incorrectly or if the player ran out of the chances given
okay students, I hope that I have made myself very clear
******************welcome to round 1************************
Hi what is your name
davey
davey you have a very unique name, my dearest
davey how old are you
12
davey come let's play
davey round 1 level 1 question 1 you are required to match the letter to the corresponding answer
davey please add 2 + 4
A:6
B:46
a
davey well done kiddo, correct is right, keep it up
round 1
level 1
the total score is 650
davey round 1 level 2 question 2 you are required to match the letter to the corresponding answer
davey please add 22 + 7
A:29
B:32
a
davey well done kiddo, correct is right, keep it up
round 1
level 2
the total score is 2600
davey round 1 level 3 question 3 you are required to match the letter to the corresponding answer
davey please add 5 + 2
A:7
B:43
b
davey no troll!! not the correct answer, you need to try again
round 1
level 3
the total score is 2375
davey round 1 level 4 question 4 you are required to match the letter to the corresponding answer
davey please add 21 + 2
A:23
B:67
a
davey well done kiddo, correct is right, keep it up
round 1
level 4
the total score is 3475
BUILD SUCCESSFUL (total time: 28 seconds)

this is were the game stop

Recommended Answers

All 5 Replies

It won't work because it's too hard to debug.
Nobody is going to debug a loop and nested ifs that stretch over 200 lines - it's just too hard. Refactor your code so the outer loop fits on one or two screenfulls. Your objective is to make each block of code really simple to understand and therefore get right.
For example: you could separate the logic of looping/ levels/re-trying etc from the actual questions by doing something like

// main loop - just the overall loop logic, no I/O here...
level = 1; chancesRemaining = 3 ;
while (level < 10)
    boolean answeredCorrectly = doLevel(level);
    if (answeredCorrectly) {
        level ++
        chancesRemaining = 3 
    } else {
        chancesRemaining --
        if (chancesRemaining <= 0) {
           // call a method to deal with user running out of chances
        }
    }
} // how hard was that to read and understand?

private boolean doLevel(int level) {
   //  do all that prompting and getting and checking answers for
   // a single question here
   // look for opportunities to eliminate repeated code by defining more methods
}  

ah will give it ah try thanks james

well jame i tried doing what you say and i am getting this error
terminaloperator mean this line:

 private boolean doLevel (int){ - error at this line
    }   


}


    boolean answercorrectly = doLevel(level); it say it is missing

 a return statement. what does that mean and how to fix it


    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
        at terminaloperator.Terminaloperator.doLevel(Terminaloperator.java:171)
        at terminaloperator.Terminaloperator.main(Terminaloperator.java:130)
    Java Result: 1

I just gave pseudo code to explain the structure, not full Java - that's up to you.

Line 1 above is NOT a valid declaration of a method, but you knew that really anyway.

I just checked a bit the original code:

while(i<=4)
        {
            if(i<=4)
            {

There is no need for the if statement here, that's already checked by the while.
Secondly, your problem lies here:

          else if(choice == 'B' || choice == 'b')
            {
                System.out.println(playername+" no troll!! not the correct answer, you need to try again");
                score -= score - 225;
                System.out.println("round 1");
                System.out.println("level "+ i);
                totalscore -= score;
                System.out.println("the total score is "+ totalscore);
                i++; // HERE
                wrongcount++;//to count all the incorrect ans
            }

for a wrong answer, you also augment i, meaning that if the value of i becomes 4 afterwards, you won't iterate over it anymore. Either don't augment i, or add an internal loop in that else block.

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.