hi all
this is my individual assignment, it is math multiple choice game program in java. it have to random numbers
got the random number but it is supposed to generated two different answer but instead it generated the same numbers for the answer, how do i fixed that.

the game started off running good all of sudden it started to loop even though i put a break statement. it will loop until the user entered the wrong answer, i have a feeling there is a logical error somewhere which is causing it to loop
can u help me with this. here is the code and the printed out output

 Scanner userinput = new Scanner (System.in);
       Random randobj = new Random();

       char choice;

       String username, welcomemsg, rulesofgme;//this is entering the username and the welcome msg

       int level, userage;
       int sumpts =0;
       int totalpts =0;
       int random = 0, random1 = 0;
       int incorrect = 0; //to hold the number of incorrect answers
       int correct = 0; // to hold the number of correct answers
       int answer = 0;// this is to hold the number of correct and incorrect answer

       //initializing the variables

        System.out.println("Welcome to Aunty Lu's Multiple Choice Game");
        System.out.println("Rules of the game");
        System.out.println("the game consist of 3 games rounds up to level 5");
        System.out.println("the age limit is 8-13");
        System.out.println("Eachplayer has about 3 chances to answer correctly");
        System.out.println("Each correct answers is rewarded with points");
        System.out.println("every incorrect answer will result points being minus");
        System.out.println("okay I hope I have made myself clear");
        //this is explainng the rules of the game
        System.out.println("*****************************************************");

        System.out.println("hi can you tell me your name");
        username = userinput.next();
        System.out.println(username+" how old are you");
        userage = userinput.nextInt();
        //asking the user to enter their namd and age

        if(userage < 8 || userage > 13)
        {
            System.out.println(" exist game");
            // this part is telling you to exit the game if the incorrect age is entered
        }
        else
        {
           System.out.println(username+"  you can play this game " + " I guess you have retain the age ");
            //this is where the correct age is entered so that the game can continue
        }
        int i =0;

        while(i < 4)//the while loop is used for 
        {//start of the while loop
            random = randobj.nextInt(25)+ 1;//this is to generate numbers from 1-25
            random1 = randobj.nextInt(9) + 1;// this will generate numbers from 1-9
            int sum = 0;
            int sum1 =0;
            System.out.println(username+" you are on round 1, level 1 "+ "can you please enter the letter which represent the correct answer");
            System.out.println(username+" what is "+random + " + "+ random1);


            sum = random + random1;//this should display the answer
            sum1 = random + random1 + random1;

            System.out.println("A:"+ sum);
            System.out.println("B:"+ sum);
            choice = userinput.next().charAt(0);

        if(choice == 'A' || choice == 'a')
        {
            System.out.println("answer s correct: D "+ username+ " you earned yourself 350 point");
            sumpts += 350;
            totalpts++;


        }
        else if (choice == 'B' || choice == 'b')
        {
            System.out.println("answer is incorrect:" + username+ " you lose 175 points"); 
            sumpts = - 175;
            totalpts++;
            break;
        }  
        the output
        Welcome to Aunty Lu's Multiple Choice Game
Rules of the game
the game consist of 3 games rounds up to level 5
the age limit is 8-13
Eachplayer has about 3 chances to answer correctly
Each correct answers is rewarded with points
every incorrect answer will result points being minus
okay I hope I have made myself clear
*****************************************************
hi can you tell me your name
sam
sam how old are you
9
sam  you can play this game  I guess you have retain the age 
sam you are on round 1, level 1 can you please enter the letter which represent the correct answer
sam what is 11 + 3
A:14
B:14
A
answer s correct: D sam you earned yourself 350 point
sam you are on round 1, level 1 can you please enter the letter which represent the correct answer
sam what is 6 + 3
A:9
B:9
A
answer s correct: D sam you earned yourself 350 point
sam you are on round 1, level 1 can you please enter the letter which represent the correct answer
sam what is 1 + 3
A:4
B:4
A
answer s correct: D sam you earned yourself 350 point
sam you are on round 1, level 1 can you please enter the letter which represent the correct answer
sam what is 17 + 5
A:22
B:22
A
answer s correct: D sam you earned yourself 350 point
sam you are on round 1, level 1 can you please enter the letter which represent the correct answer
sam what is 6 + 3
A:9
B:9
B
answer is incorrect:sam you lose 175 points
BUILD SUCCESSFUL (total time: 34 seconds)

Your problem is that you don't update the value of 'i' anywhere in your while block. This causes the loop to be repeated endlessly.
When handling your 'incorrect answer', there you have a break statement, which breaks out of the loop.

so: either update the value of i, or add a break statement in the correct block as well, but, if that would be your choice, it's easier to just remove the while loop.

int i =0;
        while(i < 4){

you initialize i to have the value 0. 0 < 4 returns true.
in your if block, there's no alteration of i, so, the next check to run: 0 < 4 (and this untill an incorrect answer is given, and break; ends the iteration)

hi stltuske
nice to hear from you, it has been quite a while thou but anyway, when i change the value of i, to lets say 1, am getting the same result, and also when i removed the break statement, it would go into an infinite loop
so what do you think i should do, do i have to keep changing the value of i

hey stultuske

i want to make it like when the user input the incorrect answer, i would like it to reiterate so that they can input the correct answer, ah try using a while loop but the compiler skip the if statements and went straight for the while loop where it was saying to re-enter your answer, i have since deleted but I would like to redo the code but dont want the compiler to skip it but want it to read it instead and ifthey entered the wrong answer then it can go to the while loop
how can i do that

move the break statement from the else block to the if block, re-compile and try again.

hi stultuske
i have another question for u. in the game which is multiple choice right, i need to use random number that would randonly generate four question to which it would ask a question such as this can the user enter the character which represent the correct answer
how much is 10 +6
the answer is something like this
A:16
B:10
to which the user would choose either A or B that suit correct answer.
i managed to get the first part(A) but it is the B part ah cant seem to figure it. it would generate the same answer as B. this is what ah use

 random = randobj.nextInt(25)+1;
            random1 = randobj.nextInt(9)+1;
            sum = 0;
            sum1= 0;
            but it would generate this as the result

            melissa what is 12 + 1

A:13
B:13
how do i generate the correct answer. one should be the right answer while the other should be the wrong answer. how do I get it to generate the correct and wrong answer for a and b
please help me

You are only showing a part of your code, not the part you need to look at, I'm afraid.

What you can do, is create a class: Question, with three instance variables:

public class Question{
    private String question, rightAnswer, wrongAnswer;
    // I know it seems wrong to use a String for your example, but if you want to use it for other types of answers as well ...

  public Question(String question, String right, String wrong){
    this.question = question;
    this. rightAnswer = right;
    this.wrongAnswer = wrong;
  }

  // getters and setters

}

In your example, it could be something like this:

int i1 = 5; 
int i2 = 7; // numbers randomy chosen between -20 and 20 (for example)
int sum = i1 + i2;
int difference = generateRandomNumberBetweenMinus20And20();

Question q = new Question("How much is " + i1 + " + " + i2 + "?", "" + sum, "" + (sum + difference));

thank you stultuske

will let you know if it work

hi stultuske
when i tried those codes above, all i got was a set of syntax errors, the compiler cannot find this symbol, that symbol, had to re-initialize some of the variables,
public class Question{-got a syntax error
int difference = generateRandomNumberBetweenMinus20And20();syntax error

Question q = new Question("How much is " + i1 + " + " + i2 + "?", "" + sum, "" + (sum + difference));the difference that is highlighted, got a syntax error that say import a field or someting,

i have since deleted the whole thing. but how do i correct those errors

hi stultuske

how do you create a class without getting any syntax errors

you'll have to actually create a method generateRandomNumberBetweenMinus20And20(); that returns an int.

I just posted that as an example, without providing the implementation.

oh
what I really want to do is to get to know how to get the program to give me the correct and wrong answer. i tried using this:
System.out.println(username+" what is "+irand1+" + "+irand2+ "? ", ""+sum, ""+ (sum + difference)); but it is giving me the fever, what I meant is it hve a syntax error underneath the difference and dont know how to get rid of it. plz help many thnks in advance
A:13
B:13

without seeing the rest of your code, and preferrably an actual description of the compiler error, at this point, I can only guess (at best)

okay stultuske here it is
i want to be able get the codes to get the right and wrong answer
something like this: what is 28+9
A:37
B:15
so that the user can be able to choose and i also want to add scores, this is where with every correct answer, the user will get point and with every wrong answer the user willl lose their points. the game also says that if the person enter the wrong age, or lose the game should end. and also it required that they have at least 4 chances to get the answer correct. i would like add something to that effect. how do i do it

 System.out.println("Welcome to Aunty Luz's Multiple Choice Math Game");
        System.out.println("The game consist of 3 round with 6 level");
        System.out.println("The age limit is 8 - 13 years ");
        System.out.println("A player wins the game if all questions are answerred correctly");
        System.out.println("The player loses the game if they run out of chances");
        System.out.println("Each player has 4 chances");
        System.out.println("Each correct answer is rewarded with a maximum of 350 points");
        System.out.println("Each incorrect answer will lose half of the alloted point given ");
        System.out.println("A report is submitted at the end of the game");
        System.out.println("A player only win the game if all 10 question are answered correctly");
        System.out.println("A player loses the game if the age is entered incorrectly or if the player ran out of the chances given");
        System.out.println("okay students, I hope that I have made myself very clear");
        //input the rules and regulation of the game 


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


        System.out.println("Hello there, what is your name");
        username = userinput.next();
        System.out.println(username+" you have a very unique name my dear");
        System.out.println("What is your age "+ username);
        userage = userinput.nextInt();
        System.out.println("hmmm nice age "+ username);
        //asking for the user's name and age


        while(userage >= 8 && userage <= 13)
        {

        //this while loop is used to check if user enter the correct age
        if(userage >=8 && userage <= 13)
        {
            //begin game 

            int random, random1;
            int i = 1;
            int irand1 = 5;
            int irand2 = 7;
            int sum = irand1 + irand2;


        while(i < 4)//this while loop is used as to 
        {
            random = randobj.nextInt(25)+ 1;//this is to generate number from 1-25
            random1 = randobj.nextInt(9)+ 1;//this is used to generate numbers from 1-9

            sum = 0;
            int sum1 =0;

            System.out.println(username+" you are on round 1, level, "+ "can you please enter the letter which represent the correct answer");
            System.out.println(username+" what is "+irand1+" + "+irand2+ "? ", ""+sum, ""+ (sum + difference));

            sum = irand1 + irand2;//this display the answer
            /*if( )*/
            {
            }
            System.out.println("A:"+sum);
            System.out.println("B:"+sum);
            choice = userinput.next().charAt(0);

        if(choice == 'A' || choice == 'a')//this if statement meant that it doesn’t matter which character the user entered
        {
            System.out.println(username+" that is the correct answer:-) "+ " you earned yourself 350 point "+ " good job, keep up the good work");
            sumpts = +350;
            totalpts_username++;
            chances++;

        }
        else if(choice == 'B' || choice == 'b')
        {
            System.out.println(username+" that answer is incorrect "+ " you lose 175 points "+ " better luck next time");
            sumpts = -175;
            totalpts_username++;
            chances++;
            i++;

        }
        while(choice != 'A' || choice != 'B')//this while loop is used to give the user another chance
        {
            System.out.println(username+ " you are given another chance "+ " make the most of it");
            System.out.println(username+" what is "+ irand1+ " + "+ irand2+ "?, "+sum, ""+ (sum + difference));
            System.out.println("A"+sum);
            System.out.println("B"+sum);
            choice = userinput.next().charAt(0);
            break;
        }


        while(i < 4)
        {
             random = randobj.nextInt(25)+ 1;//this is to generate number from 1-25
             random1 = randobj.nextInt(9)+ 1;//this is used to generate numbers from 1-9

             sum = 0;
             sum1 =0;

             System.out.println(username+" you are on round 1, leve 2, "+ "can you please enter the letter which represent the correct answer");
             System.out.println(username+" what is "+irand1+" + "+irand2+ "? ", ""+sum, ""+ (sum + difference));

            sum = irand1 + irand2;//this display the answer
            /*if( )*/
        {
        }
            System.out.println("A:"+sum);
            System.out.println("B:"+sum);
            choice = userinput.next().charAt(0);

        if(choice == 'A' || choice == 'a')
        {
            System.out.println(username+" that answer is quite correct:-)"+ " keep up the good work");
            sumpts = +352;
            totalpts_username++;
            chances++;
        }
        else if(choice == 'B' || choice == 'b')
        {
            System.out.println(username+" that is incorrect "+ " please try again");
            sumpts = -175;
            totalpts_username++;
            chances++;
            i++;
        }
        if(choice != 'A' || choice != 'B')//this if statement is checking to see if the answer entered is not equal to their answers entered
        {
            System.out.println(username+" plz try harder this time");
            System.out.println(username+" what is "+ irand1+ " + "+irand2+ "?", ""+ sum, ""+ (sum + difference));
            System.out.println("A:"+sum);
            System.out.println("B:"+sum);
            choice = userinput.next().charAt(0);
            //this here is given the user another chance to answer the question correctly
        }
         else   
        {
            System.out.println(username+"you lose the game");// this is indicating that the user has exhausted their chances

        }
            System.out.println("exit game");//this is where is the game will end if the user uses up all their chances.

        }


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

        while(i < 4)
        {
            random = randobj.nextInt(25)+1;
            random1 = randobj.nextInt(9)+1;
            sum = 0;
            sum1= 0;

            System.out.println(username+" you are on round 2, level 5 "+ " you are hereby required to entered the letter which represent the correct answer");
            System.out.println(username+" what is "+ random+ " - "+ random1);

            System.out.println("A:"+sum);
            System.out.println("B:"+sum);
            choice = userinput.next().charAt(0);

        if(choice == 'A' || choice == 'a')//this is used to say that it doesnt matter which character  is used,the answer will still display
        {
            System.out.println("correct is right "+username+ " you earned yourself 350 point "+ " well done!! keep up the good work, my little friend");
            sumpts = + 350;
            totalpts_username++;
            chances++;
            i++;
        }
        else if(choice == 'B' || choice == 'b')//the user have to opportunity to enter either of the two character the answer would remain the same
        {
            System.out.println("that is not correct "+username+ " do you want some help "+ " better luck next time " + " you just lose 175 points for that");
            sumpts = - 175;
            totalpts_username++;
            chances++;
            break;
        }
        if(choice != 'A' || choice == 'B')
        {
            System.out.println(username+" you need to try again ");//giving the user another chance to input the correct answer
            System.out.println("A:"+sum);
            System.out.println("B:"+sum);
            choice = userinput.next().charAt(0);
        }
        }//while loop end






        }
        }
        else
        {

              System.out.println("Sorry your age is invalid");

        }

     }

You are still not showing the entire code, nor are you telling us what exactly the error/exception is, and where it is thrown.

hi stultuske
that is the entire code, it is that far ah reach and the error is: System.out.println(username+" what is "+irand1+" + "+irand2+ "? ", ""+sum, ""+ (sum + difference)); the error is the difference.
the error is: add import for com.sun.apache.xalan.internal.lib.exsltSets.difference(nodelist,nodelist) that is the errror

that happens because you didn't declare difference. If you had declared it as an int, the compiler would 've known what you were talking about.

Don't just copy paste code into your code: look to both your code and the example, and see for yourself how you should implement it in there. It wasn't meant to be a complete copy paste and done option.

thnx

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.