Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

game looping until I entered the wrong answer ,need help

java

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 divinity03

divinity03

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

Posted 15 November 2015 - 04:39 PM

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;
        }  
        
        
            
        }//end of while loop
            
            
            here is 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)
            
      
        {
            
            
        }
            
            
          
        }
        
        
            
        }