This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2015/12/27

can somebody tell me what is wrong here

divinity divinity

2015/12/27

#
hi I have a few more days to hand this assignment up, so I need a answer asap. been doing this assignment. . the game has three round with 10 questions. first round 4 questions are randomly generated, second round has also 4 questions that are also randomly generated and the third round has 2 questions that are randomly generated. each rounds there are four levels. first round four levels with a question each generated. second level also four round each with a question generated and third round 2 questions generated. for each round when a question is generated and the user input the wrong answer, it is supposed to iterate to back to the same level. let say on level one the user input the wrong answer, it supposed to iterate back to level one and stay on level one until the user enters the correct answer and it is also goes for all of the levels and round. the things is it is not even iteration and when it iterate, it iterating and generating the wrong answer with the same question. i have tried everything but I cant seem to find the logical errors and i know there is logical errors. here is the codes that i hve used.
 
for(i =1; i<=4; i++)
                  {
                      //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')// indicating that any character could be entered
                      {
                        System.out.println(playername+" well done kiddo, correct is right, keep it up");
                        score += score + 650;
                        System.out.println("round 1");//indicating the round the user is on
                        System.out.println("level "+ i);//indicating the level the player is on
                        totalscore += score;//adding the scored
                        System.out.println("the total score is "+ totalscore);
                        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");//letting the user know they have entered the wrong answer
                        score -= score - 225;
                        System.out.println("round 1");
                        System.out.println("level "+ i);
                        totalscore -= score;
                        System.out.println("the total score is "+ totalscore);
                        wrongcount++;//to count all the incorrect ans
                    }
                }  
                  i=1;
              /*while(wrongans <4)
              {
                    System.out.println(playername+"  round 1 "+ "level "+ i +" question "+ i +" you are required to match the letter with the corresponding answer");
                    System.out.println(playername+" please add "+ rand + " + "+ rand2);

                    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);
                        correctans++;
                    }
                    else if(choice == 'B' || choice == 'b')
                    {
                        System.out.println(playername+" no you troll that is incorrect, 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);
                        wrongcount++;
                    }
              }
              System.out.println("**********************welcome to round 2********************************");
              
              for(i = 5; i<=9; i++)
              {
                  //get random numbers
                  largest = 1 + randobj.nextInt(25);
                  smallest = 2 + randobj.nextInt(8);
                  correctans =largest - smallest;
                  incorrectans = 25 + randobj.nextInt(50);
                  
                  for(i =1; i<=4; i++)
                  {
                      if(i == 1)
                      {
                          smallest = num2;
                          largest = num1;
                      }
                      if(num1 < smallest)
                      {
                          smallest = num2;
                      }
                      if(num1 > largest)
                      {
                          largest = num1;
                      }
                  }
                  System.out.println(playername+"round 2 "+ " level "+ i + " question "+ i + " you are required to match the letter with the matching answer");
                  System.out.println(playername+" the largest and smallest number entered are: please subtract "+ largest + " - "+ smallest);
                  
                  System.out.println("A:"+incorrectans);
                  System.out.println("B:"+correctans);
                  choice = userinput.next().charAt(0);
                  
                    if(choice == 'A' || choice == 'a')
                    {
                        System.out.println(playername+" that is not cool, the answer is incorrect,you can do better than that");
                        score -= score -225;
                        System.out.println("round 2");
                        System.out.println("level "+ i);
                        totalscore -= score;
                        System.out.println("the total score is "+ totalscore);
                        wrongcount++;
                    }
                    else if(choice == 'B' || choice == 'b')
                    {
                        System.out.println(playername+" great well done kiddo, that is so correct, you are doing wonderful");
                        score += score + 650;
                        System.out.println("round 2");
                        System.out.println("level "+ i);
                        totalscore += score;
                        System.out.println("the total score is "+ totalscore);
                        correctans++;
                    }
                    
                 
                   while(wrongans < 4)
                   {
                       System.out.println(playername+" round 2 "+ " level "+ i +" question "+ i + "please match the letter with the correspondng answer");
                       System.out.println(playername+" the largest and smallest number entered are: please subtrac "+ largest+ " - "+smallest);
                       
                       System.out.println("A:"+incorrectans);
                       System.out.println("B:"+correctans);
                       choice = userinput.next().charAt(0);
                       
                       if(choice == 'A'|| choice == 'a')
                       {
                           System.out.println(playername+" that is not cool, the answer is incorrect, you can do better than that");
                           score -= score -225;
                           System.out.println("round 2");
                           System.out.println("level "+ i);
                           totalscore -= score;
                           System.out.println("the total score is "+ totalscore);
                           wrongcount++;
                       }
                       else if(choice == 'B' || choice == 'b')
                       {
                           System.out.println(playername+" great job, well done kiddo, that is so correct, you are doing wonderful");
                           score += score + 650;
                           System.out.println("round 2");
                           System.out.println("level "+ i);
                           totalscore += score;
                           System.out.println("the total score "+ totalscore);
                           correctans++;
                       }
                   }
              
              }
              System.out.println("********************welcome to round 3*******************************");
              
              for(i = 9; i<=10; i++)
              {
                  //get the random numbers
                  numerator = 1 + randobj.nextInt(25);
                  denominator = 2 + randobj.nextInt(8);
                  num1 = 2 + randobj.nextInt(20);
                  num2 = 2 + randobj.nextInt(25);
                  correctans = numerator = denominator * num2;
                  correctans = numerator / denominator;
                  incorrectans = 25 + randobj.nextInt(50);
                  
                  if(numerator %2!=denominator)
                  {
                        //letting the players know of the round and level they will be starting at
                        System.out.println(playername+" round 3 "+" level "+ i +" question "+ i + " you are to match the letter with the matching answer");
                        System.out.println(playername+" please divide "+ numerator + " / "+ denominator);

                        System.out.println("A:"+correctans);
                        System.out.println("B:"+incorrectans);
                        choice = userinput.next().charAt(0);
                      
                        if(choice == 'A' || choice == 'a')
                        {
                            System.out.println(playername+" yea man daiz wey we taling about, that is correct, a job well done");
                            score += score +650;
                            System.out.println("round 3");
                            System.out.println("level "+ i);
                            totalscore += score;
                            System.out.println("the total score is "+ totalscore);
                            correctans++;
                        }
                        else if(choice == 'B' || choice == 'a')
                        {
                            System.out.println(playername+" no that is not the correct answer, you need to buck up on your maths");
                            score -= score - 225;
                            System.out.println("round 1");
                            System.out.println("level "+ i);
                            totalscore -= score;
                            System.out.println("the total score is "+ totalscore);
                            wrongcount++;
                        }
                        
                       
                        while(wrongans < 4)
                        {
                            System.out.println(playername+" round 3 "+ " level "+ i +" question "+ i + " you are required to match the letter with the corresponding answer");
                            System.out.println(playername+" please divide "+ numerator + " / "+ denominator);
                            
                            System.out.println("A:"+correctans);
                            System.out.println("B:"+incorrectans);
                            choice = userinput.next().charAt(0);
                            
                            
                            if(choice == 'A' || choice == 'a')//this indicationg any character can be used
                            {
                                System.out.println(playername+" aaawwwsome that is so correct, you are doing very good keep it up");
                                score += score +650;
                                System.out.println("round 3");
                                System.out.println("level "+ i);
                                totalscore += score;
                                System.out.println("the total score is "+ totalscore);
                                correctans++;//thi counting all the correct ans
                            }
                            else if(choice == 'B' || choice == 'b')
                            {
                                System.out.println(playername+" aww come on!!! you can do better, that is not correct");
                                score -= score - 225;
                                System.out.println("round 3");
                                System.out.println("level "+ i);
                                totalscore -=score;
                                System.out.println("the total score "+ totalscore);
                                wrongcount++;
                            }
                            
                            if(correctans > totalscore)
                            {
                                System.out.println(playername+" you won the game");
                            }
                            else if(correctans < totalscore)
                            {
                                System.out.println(playername+" you lose the game");
                            }
                            else if(correctans == totalscore)
                            {
                                System.out.println(playername+" game draw");
                            }
                            totalscore = correctans + totalscore;
                            System.out.println("the total score is "+ totalscore);
                            
                            System.out.println("the player name is "+ playername);
                            System.out.println("the player age is "+ playerage);
                            System.out.println("the question is "+ question);*/
                            
                            
                            
danpost danpost

2015/12/27

#
The 'for' loop will always iterate to the next control value on each pass (whether the answer was correct or incorrect). To maintain the same value on an incorrect answer, you need to decrement the control value before iterating. In other words, in all blocks executing code for an incorrect answer (i.e. the block between lines 29 and 37), add the following line:
i--;
divinity divinity

2015/12/27

#
hi danpost it is in a loop in level 5 which is not supposed to do. level 5 is for the second round not the first round. when the user entered the correct answer it is iterating in a infinite loop and break out only when the user enter the wrong answer. which again not supposed to. it is should iterate only when the user entered the wrong on like level 4 and it should remain on level and repeat the same question until the correct answer is entered. and it also generating the wrong answer which should not be. one answer should be correct and another is incorrect. here is the output.
Hi what is your name
glen
glen you have a very unique name, my dearest
glen how old are you
13
glen come let's play
glen round 1  level 1 question 1 you are requred to match the letter with the correspnding answer
glen please add 3 + 1
A:4
B:40
a
glen wow, well done kiddo, correct is right, you did well
round 1 
level 1
the total score is 650
glen round 1  level 2 question 2 you are requred to match the letter with the correspnding answer
glen please add 13 + 7
A:20
B:66
b
glen no this is not the correct answer, you need to buck up, try again
round 1
level 2
the total score is 425
glen round 1  level 3 question 3 you are requred to match the letter with the correspnding answer
glen please add 15 + 8
A:23
B:69
a
glen wow, well done kiddo, correct is right, you did well
round 1 
level 3
the total score is 1525
glen round 1  level 4 question 4 you are requred to match the letter with the correspnding answer
glen please add 12 + 2
A:14
B:55
a
glen wow, well done kiddo, correct is right, you did well
round 1 
level 4
the total score is 4375
glen round 1 level 5 question 5 you are required to match the letter to the corresponding answer: it should only iterate when the wrong answer is incorrect not when it is correct.
glen please add 12 + 2
A:14
B:55
b
glen no troll!! not the correct answer, you need to try again
round 1
level 5
the total score is 4150
BUILD SUCCESSFUL (total time
divinity divinity

2015/12/27

#
hi danpos this what I am trying to say. on level 3 is where the wrong answer was entered and it iterate to level 1 and generate level 4 question. and it is not breaking out of the loop unless a wrong answer is entered. and you see where the level 4 question is generated and check the answer which is the wrong answer. in level 3 where the wrong answer was entered by the used, the game was supposed to iterate on level 3 and generate that same question and it supposed t remain on level 3 until the user entered the correct answer. this is one of the many problems with the game. here is another output when i ran it again. u will see where it is repeating level 4 question and where it has the wrong answer here is the second output
Hi what is your name
danicia
danicia you have a very unique name, my dearest
danicia how old are you
12
danicia come let's play
danicia round 1 level 1 question 1 you are required to match the letter to the corresponding answer
danicia please add 11 + 1
A:12
B:53
a
danicia well done kiddo, correct is right, keep it up
round 1
level 1
the total score is 650
danicia round 1 level 2 question 2 you are required to match the letter to the corresponding answer
danicia please add 14 + 1
A:15
B:47
a
danicia well done kiddo, correct is right, keep it up
round 1
level 2
the total score is 2600
danicia round 1 level 3 question 3 you are required to match the letter to the corresponding answer
danicia please add 10 + 4
A:14
B:52
b
danicia no troll!! not the correct answer, you need to try again
round 1
level 3
the total score is 2375
danicia round 1 level 4 question 4 you are required to match the letter to the corresponding answer
danicia please add 19 + 8
A:27
B:58
a
danicia well done kiddo, correct is right, keep it up
round 1
level 4
the total score is 3475
danicia  round 1 level 1 question 1 you are required to match the letter with the corresponding answer
danicia please add 19 + 8
A:28
B:58
b
danicia no you troll that is incorrect, you need to try again
round 1
level 1
the total score is 3250
danicia  round 1 level 1 question 1 you are required to match the letter with the corresponding answer
danicia please add 19 + 8
A:28
B:58
a
danicia well done kiddo, correct is right, keep it up
round 1
level 1
the total score is 4350
danicia  round 1 level 1 question 1 you are required to match the letter with the corresponding answer
danicia please add 19 + 8
A:27
B:58
a
danicia well done kiddo, correct is right, keep it up
round 1
level 1
the total score is 7200
danicia  round 1 level 1 question 1 you are required to match the letter with the corresponding answer
danicia please add 19 + 8
A:26
B:58
b
danicia no you troll that is incorrect, you need to try again
round 1
level 1
the total score is 6975
danicia  round 1 level 1 question 1 you are required to match the letter with the corresponding answer
danicia please add 19 + 8
A:26
B:58
danpost danpost

2015/12/28

#
I think you not only need to track which level has been set up so it is only done once per level, but need to reset the level internally upon an incorrect answer. Something along these lines:
int setup = 0;
for (int i=1; i<=4; i++)
{
    if (setup < i)
    {
        // set up (like lines 3 through 16 above)
        setup++;
    }
    choice = userinput.next().charAt(0);
    if (choice == 'A' || choice == 'a')
    {
        // correct stuff (like lines 20 through 26)
    }
    else (choice == 'B' || choice == 'b')
    {
        // incorrect stuff (like lines 30 through 36)
        i--;
    }
    else i--; // invalid key entry (not 'a' or 'b')
}
divinity divinity

2015/12/28

#
hi danpost i understand the rest but it is the
if(setup<i)
i dont understand
divinity divinity

2015/12/28

#
hey danpost correct me if i am wrong, the example that is shown above is supposed to be done something like this
for(i=1; i<=4;i++)
{
   if(chances < i)
}
choice = userinput.next().charAt(0);
if(choice == 'A' || choice == 'a'
am i to understand it can be something like that
danpost danpost

2015/12/28

#
divinity wrote...
i understand the rest but it is the
if(setup<i)
i dont understand
It is supposed to track the value of 'i' during the looping (or, how high it has been). This is to prevent the set-up code (generating of random values, among other things) from re-executing when an incorrect answer is given. You need to include your current code in those places where my full comment lines are. I indicated what lines should be placed there.
divinity divinity

2015/12/28

#
it is iteration four time which is not supposed to do. it only supposed to iterate when the user enter the wrong answer
divinity divinity

2015/12/28

#
I did follow you example and as i say above, it is iteration four time, meaning it is repeating from level 1 to 4 again.
divinity divinity

2015/12/28

#
i am trying to implement a number of tries the user has to re-enter the correct answer. i want it to be something like this
if(chanceRemaining == 4)
{
printf(playername+ you have xx number of tries left)
}
this should be where the user has the opportunity to enter the correct answer
danpost danpost

2015/12/28

#
divinity wrote...
it is iteration four time which is not supposed to do. it only supposed to iterate when the user enter the wrong answer
Show your revised code for correction. Just saying it is does not do what it supposed to do does not help.
divinity divinity

2015/12/29

#
hi danpost I tried following your example but it is iteration four times starting from level 1 - 4 with level 4 question.
 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
        }
        
       
         for(i =1; i<=4; i++)
         {
             //get random number
             rand  = 1 + randobj.nextInt(25);
             rand2 = 1 + randobj.nextInt(8);
             correctans = rand + rand2;
             incorrectans = 25 + randobj.nextInt(50);
             
             //indicating 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 matching answer");
             System.out.println(playername+" please add "+ rand +" + "+ rand2);
             
             System.out.println("A:"+correctans);
             System.out.println("B:"+incorrectans);
             choice = userinput.next().charAt(0);
             
            if(choice == 'A'|| choice == 'a')
             {
                 System.out.println(playername+" excellent 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 "+ totalscore);
                 correctans++;
                
            }
            else if(choice == 'B'|| choice == 'b')
            {
                System.out.println(playername+" awww come one now, that is not the correct answer");
                score -= score - 225;
                System.out.println("round 1");
                System.out.println("level "+ i);
                totalscore -= score;
                System.out.println("the total score is "+ totalscore);
                wrongcount++;
                
            }
         }  
             int wrongans = 0;
             for(i = 1; i<=4; i++)
             {
                    if(wrongans < i)
                    {
                     System.out.println(playername+" round 1 "+ " level "+ i + " question "+ i + " you are required to match the letter to the matching answer");
                     System.out.println(playername+" please add "+ rand +" + "+ rand2);

                     System.out.println("A:"+correctans);
                     System.out.println("B:"+incorrectans);
                     wrongans++;
                    }
                      choice = userinput.next().charAt(0);
                      if(choice == 'A'|| choice == 'a')
                      {
                          System.out.println(playername+" excellent, kiddo you are doing well");
                          score += score + 650;
                          System.out.println("round 1");
                          System.out.println("level "+ i);
                          System.out.println(" chances "+ wrongans);
                          totalscore += score;
                          System.out.println("the total score are "+ totalscore);
                          wrongans--;
                      }
                      else if( choice == 'B' || choice == 'a')
                      {
                          System.out.println(playername+" come on now, you can do better than that");
                          score -= score - 225;
                          System.out.println("round 1");
                          System.out.println("level "+ i);
                          System.out.println("chances "+ wrongans);
                          totalscore -= score;
                          System.out.println("the total score is "+totalscore);
                          wrongans--;
                          
                      }
             }
             
any assistance will be greatly appreciated. thanks i have one more day to hand this up
danpost danpost

2015/12/29

#
I guess you did not notice my lines 17 and 19. You need to decrement 'i' upon any wrong keystroke so that the code can be rerun for the same question. You also still need the 'setup' field to track the number of times the set-up code (for each question) has been executed and avoid a NEW question from being asked on an incorrect answer
You need to login to post a reply.