Click here to Skip to main content
15,891,712 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi: I am building my final project and i know there is logical errors in it to which i cannot see. the game goes like this: it is a multiple choice math java game. it has three rounds. addition subtraction and division. i have already gotten the addition round working like it supposed to. it is the subtraction and the division rounds is where there is the logical error. for some reason it is skipping the second rounds and going straight to the third rounds. in round three it is generating zero for the user to divide and it must not generate zero and another thing is that it supposed to say round 3 level 9 instead it is saying round 3 level 4. can somebody help me fix this

some logical error causing to skip the second round and another one causing it to generate zero and the level.

What I have tried:

Java
int num ;
            
            while(i>=5 && i<=8 && wrongcount <4)
            {
                i++;
                //get random numbers
                largest  = 1 + randobj.nextInt(25);
                smallest = 2 + randobj.nextInt(9);
                correct_ans = largest - smallest;
                incorrect_ans = 1 + randobj.nextInt(35);
                
                while(correct_ans == incorrect_ans)
                {
                    incorrect_ans = 1 + randobj.nextInt(35);
                }
                if(i == 1)
                {
                    //getting the largest and smallest number 
                    largest  = num1;
                    smallest = num2;
                }
                else
                {
                    if(num1 > largest)
                    {
                       largest = num1;
                    }
                    if(num2 > smallest)
                    {
                       smallest = num2;
                    }
                }
                //indicate the round and level would be starting from
                System.out.println(playername+" round 2 "+" level "+ i +" match the letter with the correct answer");
                System.out.println(playername+" please subtract "+ largest + " - "+smallest);
                
                System.out.println("A:"+correct_ans);
                System.out.println("B:"+incorrect_ans);
                uSelect = reader.next().charAt(0);
                
                if(uSelect == 'A' || uSelect == 'a')
                {
                    System.out.println(playername+"awwwwsome, well done,  answer is correct, you are doing good, keep up the good work");
                    totalscore +=350;
                    System.out.println("round 2");
                    System.out.println("level "+ i);
                    totalscore +=score;
                    System.out.println("total score it"+totalscore);
                }
                else 
                {
                    while(uSelect != 'A' && uSelect != 'a' & wrongcount < 4)
                    {
                        System.out.println(playername+" awww come on now wrong answer, let's do this again ");
                        totalscore -=175;
                        System.out.println("round 2");
                        System.out.println("level "+ i);
                        totalscore -=score;
                        System.out.println("total score:"+totalscore);
                        
                        if(wrongcount <4)
                        {
                            System.out.println(playername+" round 2 "+ "level "+ i+" match the letter with the correct answer");
                            System.out.println(playername+" please subtract "+ largest + " - "+ smallest);
                            
                            System.out.println("A:"+correct_ans);
                            System.out.println("B:"+incorrect_ans);
                            uSelect = reader.next().charAt(0);
                            
                        }
                    }
                }
            }
           
            
            System.out.println("\n*******************welcome to round***3*divide**round****************************");
            
            
            while(i>=9 && i<=10 && wrongcount < 4)
            {
                i++;
                //get random numbers
                numerator   = 1 + randobj.nextInt(25);
                denominator = 2 + randobj.nextInt(9);
                numvalue  = 1 + randobj.nextInt(25);
                numvalue2 = 2 + randobj.nextInt(9);
                correct_ans = numerator = denominator * numvalue2;
                correct_ans = numerator / denominator;
                incorrect_ans = 1 + randobj.nextInt(35);
                
                while(correct_ans == incorrect_ans)
                {
                    incorrect_ans = 1 + randobj.nextInt(35);
                }
                System.out.println(playername+" you are on round 3 "+" level "+ i + " match the letter with the correct answer");
                System.out.println(playername+" please divide "+ numerator + " / "+ denominator);
                
                System.out.println("A:"+incorrect_ans);
                System.out.println("B:"+correct_ans);
                uSelect = reader.next().charAt(0);
                
                if(uSelect == 'B' || uSelect == 'b')
                {
                    System.out.println(playername+" hey that's great, correct is right, keep up the good worl");
                    totalscore +=350;
                    System.out.println("round 3");
                    System.out.println("level "+ i);
                    totalscore +=score;
                    System.out.println("total score:"+totalscore);
                }
                else
                {
                    while(uSelect !='B' && uSelect !='b' && wrongcount < 4)
                    {
                        System.out.println(playername+" tsk, tsk, nice bangbang!!! that is wrong, please try again, kiddooo!!!");
                        totalscore -=175;
                        System.out.println("round 3");
                        System.out.println("level "+ i);
                        totalscore -=score;
                        System.out.println("total score: "+totalscore);
                        
                    }
                }
                
            }
            while(wrongcount < 4)
            {
                System.out.println(playername+" you are on round 3 "+ " level "+ i + " match the letter with the correct answer");
                System.out.println(playername+" please divide "+ numerator + " / "+ denominator);
                
                System.out.println("A:"+incorrect_ans);
                System.out.println("B:"+correct_ans);
                uSelect = reader.next().charAt(0);
                
            }
        }    
        else
        {
            System.out.println(playername+"that is not a valid character");
            System.exit(0)
Posted
Updated 25-Apr-16 22:50pm
v2
Comments
Sergey Alexandrovich Kryukov 25-Apr-16 23:09pm    
Debugger!
HobbyProggy 26-Apr-16 2:55am    
In my opinion, from what i could identify without knowing the complete code i'd say you increase your i on the wrong place. Like you enter the while loop with 9 and immedeatly jump to 10?

Secondly you should try debugging it step by step. Then you could check the current value of i at your println and check when i gets the wrong values.

1 solution

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

It is not complete code, but it looks like the second round is done only if you give wrong answer to first one.
 
Share this answer
 
Comments
divinity02 26-Apr-16 5:21am    
thank ppolymorphe, will do

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900