I ave ah questions it is just a question, so dont penalized me okay
question 1: how do you spot logic errors
question 2: how do you fix it
question 3: what can be done to try to prevent it, is there a way

1: You get an exception (null pointer, zero divide etc)
or
the program behaves in a way didnlt expect (wrong output etc)

2: Trace the execution one step at a time, using a debugger or lots of print statements until you find where its going wrong

3: Design top-down - classes / method signatures etc before getting into details, so any logic errors will be localised
and
keep methods small, doing just one thing
and
be very careful with naming methods/variables etc so the name exactly describes the usage, and so any mis-use is obvious

am not getting the output that I was expecting.
it is reading one statement instead of the other, it is not looping back like it supposed to
it is skipping a whole block of statement
it is jumping to the else statement when it should be reading the if when you entered the correct answer
and no am not getting the exception error at all. funny

2: Trace the execution one step at a time, using a debugger or lots of print statements until you find where its going wrong

(sounds like there are logical expressions in if tests or loops that are not evaluating to the result you expect - so print them and all the variables used in them)

Edited 1 Year Ago by JamesCherrill

The biggest issue that I have found is ensuring that data types match or they will never be equal. @JamesCherrill makes a great point about printing values; it is a great tool but, be aware that 1 is not equal to "1"

here is the code, you all can take a look at it and tell me what i am doing wrong I have debug it many times, but i have seen where it was skipping statement (dont know why it was doing that)

 int i = 1;//to keep track the  12 cycles
        int count =0;
        int score =0; //to keep track of the scores in the game
        int quest1 =0, quest2 = 0;
        int ans = 0, numtries = 3; //is used to store answers from the players and the amt of tries
        int countlev_easy = 0,countlev_med = 0,countlev_hard = 0;
        int user2age = 0, num1 = 0, num2 = 0;
        String user2name ="";
        int level =0,sumpts = 0, str1=0, str2=0;
        int passmark = 50;
        int rand1 = 0, rand2 = 0,  rand;
        int countlevel =0;
        int totalscore = 0, correct = 0, incorrect = 0;
        String choose = null;


        //introduction to the game and the rules
        System.out.println("Welcome ladies!!!!!! Aunty Luana Multiplication Game");
        System.out.println("This is where you ladies will learn the multiplication table a new way");
        System.out.println("The new is by way of a game, a multiplication game quiz");
        System.out.println("we are going to have some fun while learning at the same time");
        System.out.println("Are you ready ladies!!!");

        //this is the rules of the game
        System.out.println("The games game consisit of three round with 5 levels");
        System.out.println("The levels are made up of easy, medium and hard");
        System.out.println("You have the choice of choosing which level of the game you want to play, ok");
        System.out.println("In this game, you have four chance of getting the correct answer");
        System.out.println("The game has an age limit, you have to between the ages of 6 and 12");
        System.out.println("The game will be forfeited if the criteria of the game dont meet");
        System.out.println("This is a two players game, each player will be given their chance to answer correct");
        System.out.println("To every correct answer, you are given point, and every incorrect answer, points will be taken away");
        System.out.println("who ever scored the highest point win the game or if anyone loose their chances by giving the incorrect answer, the other player wins by default");
        System.out.println("I hope that I have made myself clear");
        System.out.println("so here we go, Good Luck!!!!!!!!!!!!");

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

        //while loop prompting the user to state which level of the game they would like to play
        while(countlev_easy == 'e' ||countlev_easy == 'E' || countlev_med == 'm' || countlev_med == 'M'|| countlev_hard == 'h'|| countlev_hard == 'H')
        {

           System.out.println("which level would you like to start with");
           System.out.println(choose+ "e " + choose+" M");
           System.out.println(choose +" m " + choose + "M");
           System.out.println(choose+ "h " + choose+ "H");
           countlevel = userinput.nextInt();
           (it is skipping this whole block of code, the compile not reading this at all)
        }

        //here is where you are asking the user for information pertaining to their name and age

          System.out.println("hi can you  tell me your name");
          String user1name = userinput.next();
          System.out.println("what a lovely name, my dear " +user1name+ " nice love");
          System.out.println("what is your age " + user1name);
          int user1age = userinput.nextInt();

        while(user1age < 6 || user1age > 12)
        {
           System.out.println("incorrect age, you cant play this game " +user1name);
           break;

        }
        while(user1age > 6 || user1age < 12)
        {
          System.out.println("hi please re-enter your age again " +user1name);
          user1age = userinput.nextInt();


          System.out.println("boom you can play");
          break;
          }

          System.out.println("hi what is your name");
          user2name = userinput.next();
          System.out.println("wonderful, you have such unique name "+user2name);
          System.out.println("hi how old are you " +user2name);
          user2age = userinput.nextInt();

          while(user2age < 6 || user2age > 12)
        {
          System.out.println("incorrect age " +user2name+ " you cannot play this game, okay");
          break;
        }
          while(user2age > 6 || user2age < 12)
        {
          System.out.println("please re-enter your age "+user2name);
          user2age = userinput.nextInt();

          System.out.println("perfect, just the age, you can now play");
          break; 

        }
          System.out.println(user1name +  user2name+"  welcome students to round 1");

          System.out.println(user1name+  " you are the first one up");

          Random randomgenerator = new Random();
          num1 = (int)(Math.random()* 11) + 2;
          num2 = (int)(Math.random()* 11) + 3;
          int b = num1 * num2;


        while(countlevel <= 12 && countlevel <=12)
        {
          if(correct == 5 && incorrect == 5)
        {
           System.out.println("sorry but you lost the game");
        }

         System.out.println(num1+ "*"+ num2 + "=");
         ans = userinput.nextInt(); 


        if(ans == num1 * num2)
        {
          System.out.println(user1name+"  lovely,  correct " +level+ " you gain 350 points " + " you get a chance to move to the next level" + rand1);
          sumpts+=350;
          quest1++;
          numtries++;

        }

        else if(num1 * num2 != ans)
        {
          System.out.println(user1name+ "  well, that's interesting "+ "you got it incorrect "+ "  you just lost 75 points " + " you cannot move on to the next level");
          sumpts -= 175;
          quest1++;
          numtries++;
          System.out.println(user1name+ " your total score is " + totalscore);
        }
        if(totalscore > 175)
        {
          System.out.println(user1name+ " you can move on to the next level");
        }
        else if(totalscore < 175)
        {
           System.out.println(user1name+ " you are not going on to the next level");
        }

        System.out.println(user2name+" it is your turn now");


          num1 = (int)(Math.random()*12)+2;
          num2 = (int)(Math.random()*12)+2;
          int d = num1 * num2;


        while(quest1 <= 12 && quest2 <=12)
        {
          System.out.println(user2name+" how much is " + num1 + "*"+ num2+ "=");
          ans = userinput.nextInt();
          break;
        }
        if(ans == num1 * num2)
        {
            System.out.println(user2name+" awesome!!!!, that's so correct "+level+ " you gain 350 point" + " you can move on the next level " + rand2);
            sumpts +=350;
            quest2++;
            numtries++;
            break;

        }
        else if(num1 * num2 !=ans)
        {
            System.out.println(user2name+" ugh no troll incorrect, try again "+ " you lost 175 point and may not be able to move to the next level");
            sumpts -=175;
            quest2++;
            numtries++;
            System.out.println(user2name+"your total score is " + totalscore);
        }
          if(totalscore > 175)
        {
            System.out.println(user2name+ " you are more than eligible to move on to the next level");
        }
        else if(totalscore < 175)
        {
           System.out.println(user2name+ " you cannot move to the next level ");
        }

          num1 = (int)(Math.random()*12)+2;
          num2 = (int)(Math.random()*12)+2;
          int e = num1 * num2;
        }

        System.out.println(user1name+" it is your turn again");


        while(quest1 <= 12 && quest2 <=12)
        {
            System.out.println(user1name+ " how much is "+ num1 + " * "+ num2+ "=");
            ans = userinput.nextInt();
            break;
        }
        if(ans == num1 * num2)
        {
            System.out.println(user1name+" well, finally!!, you got it correct " +level+ " you have gain 350 point "+ " keep up the good work " + rand1);
            sumpts+=350;
            quest1++;
            numtries++;
        }
        else if(num1 * num2 !=ans)
        {
            System.out.println(user1name+" oh learn your table!!! incorrect again "+level+ " you lost 175 point "+ " try and do better the next time around" +rand1);
            sumpts -=175;
            quest1++;
            numtries++;
            System.out.println(user1name+ " your total score " + totalscore);
        }
        if(totalscore > 175 )
        {
            System.out.println(user1name+ " you can move to the next level " + " keep it up");
        }
        else if(totalscore < 175)
        {
            System.out.println(user1name+ " oh hell no, you cannot move on to the next level " + "you did not do well, try again");
        }

         System.out.println(user2name+" it is your turn now, again");


          num1 = (int)(Math.random()*12)+3;
          num2 = (int)(Math.random()*12)+3;
          int c = num1 * num2; 

          while(quest1 <= 12 && quest2 <=12)
        {
            System.out.println(user2name+" how much is " + num1 + "*"+ num2+ "=");
            ans = userinput.nextInt();
            break;
        }
          if(ans == num1* num2)
        {
            System.out.println(user2name+"  well done, I must say you have improved a lot, correct "+level+ " you gain 350 point " + " keep it up " + rand2);
            sumpts+=350;
            quest2++;
            numtries++;

        }
          else if(num1* num2 != ans)
        {
            System.out.println(user2name+"  ahhh come on nah man!! dont disappoint me now, it is incorrect "+level+ " you lose 175 point "+ "  try harder next time " + rand2);
            sumpts-=175;
            quest2++;
            numtries++;
            System.out.println(user2name+"your total score is " + totalscore);

        }
          if(totalscore > 75)//this is testing to see if sumpts matches total score make in the game
        {
            System.out.println(user2name+ " you will be be able to move to the next level ");
        }
        else if(totalscore < 75)
        {
            System.out.println(user2name+ " damn!!! " + " you have not improve a bit, buckle up");
        }
        here is the output
        hi can you  tell me your name
anthony
what a lovely name, my dear anthony nice love
what is your age anthony
5
incorrect age, you cant play this game anthony
hi please re-enter your age again anthony
11
boom you can play
hi what is your name
tevin
wonderful, you have such unique name tevin
hi how old are you tevin
15
incorrect age tevin you cannot play this game, okay
please re-enter your age tevin
11
perfect, just the age, you can now play
anthonytevin  welcome students to round 1
anthony you are the first one up
10*7=
71
anthony  well, that's interesting you got it incorrect   you just lost 75 points  you cannot move on to the next level
anthony your total score is 0
anthony you are not going on to the next level
tevin it is your turn now
tevin how much is 3*4=
11
tevin ugh no troll incorrect, try again  you lost 175 point and may not be able to move to the next level
tevinyour total score is 0
tevin you cannot move to the next level 
6*11=
66
anthony  lovely,  correct 0 you gain 350 points  you get a chance to move to the next level0(this part here is where user1name answer the correct answer and it read the statement for the second user
anthony you are not going on to the next level
tevin it is your turn now
tevin how much is 6*4=
24
tevin awesome!!!!, that's so correct 0 you gain 350 point you can move on the next level 0
anthony it is your turn again
anthony how much is 6 * 4=
22
anthony oh learn your table!!! incorrect again 0 you lost 175 point  try and do better the next time around0
anthony your total score 0
anthony oh hell no, you cannot move on to the next level you did not do well, try again
tevin it is your turn now, again
tevin how much is 13*3=
39
tevin  well done, I must say you have improved a lot, correct 0 you gain 350 point  keep it up 0
tevin damn!!!  you have not improve a bit, buckle up
BUILD SUCCESSFUL (total time: 2 minutes 23 seconds)

Hi

This is not a language that I am familiar with, but in line 6 you have
int countlev_easy = 0,countlev_med = 0,countlev_hard = 0;

In the test you appear to be comparing these variables with letters, not numbers ..........

Edited 1 Year Ago by DenisOxon

Hi Dennis
Yes, you're right.
char types in Java are numeric Unicode values, so it's valid to compare them with any other numeric type, but '0' is a char constant whose value is the Unicode for the letter '0', which is not the same as the number 0.

The code, sadly, has far bigger problems. Those vars look like counters, but are used to control a loop by testing them as characters, then they are never changed in the loop, so it's a good thing it never starts looping! I have no idea what it was supposed to do, but changing "it" to string is surely a random move in the wrong direction.

Your first question: how do you spot logical errors? Answer: by testing.
line 65:while(user1age > 6 || user1age < 12)
Did you test that? Try user1age = -123 what about user1age = 9999 ?
You will find out that any value will do here. Endlessly...
Consider using the AND operator, and what about the "ladies" who happen to be 6 or 12 year exactly. Can they play also?

This article has been dead for over six months. Start a new discussion instead.