Thursday, July 9, 2009

How do I design a loop to give someone 3 tries to get a math problem right in C#.?

I am developing a math tutor. I want to give the kids 3 chances to get the problem right. I will then count it wrong and move on. My loop doesn't stop to see the second and third chance. There must be a way to run the loop three times and stop and check validation each time. I am a C# student. Any help would be appreciated.


Thanks,


Tom H

How do I design a loop to give someone 3 tries to get a math problem right in C#.?
Just use a for statement whith two conditional variables where one of these variables would be a counter that has to be lower than 3 in order to continue in that question. And the other variable that controls this loop for is a boolean variable that controls if the user gives a right or wrong answer and inside of this loop you make the question and if the user chooses the right answer you change the boolean var to true and the program moves to the next question.
Reply:Let's assume the answer is 5.


In Basic:


10 x=0


20 for i=1 to 3


30 input " enter the answer":x


35 next i


37 when i%26gt;3 then goto 500


40 if x = 5 then print "Well done"


50 if x%26lt;%26gt;5 then goto 10


500 next question





It's a while since I programmed but i hope this helps.
Reply:Something like this would work.





answerRight = 0;


answers = 0;





while (answers%26lt;3 %26amp;%26amp; answerRight == 0)


{





if(student answers right)


answerRight = 1





answers ++;


}





If(answerRight == 1)


student got it right


else


student failed
Reply:The syntax may not be 100% now as I am not having a compiler in front of me now but follow the logic:





//////////////////////////////////////... Code starts here //////////////////////////////////


// True or False varaible to check if the question


//has been answered correctly


boolean correct = false;


// Variable to track number of tries


int count = 0;


// Other declarations code of questions and answers


...


// The following loop wil do the testing of three tries


while(count %26lt; 3){


// Place the code for the questions


...


// Get the anwswer


...


// Check if the correct answer was given and


//set correct variable accordingly


...


if(correct == true){


// if the answer is correct, exit the loop


break;


}





// If the answer was incorrect, increment the count of tries


// and the loop will run until count = 3


count++;


}





// Display that tries are up and give the correct answer


...





///////////////////////// Code end here ///////////////////////////////////////
Reply:boolean isAnswerCorrect = false;


boolean keepAskingQuestion = true;


int askedQuestionCounter = 1;





while (keepAskingQuestion == true)


{


//ask question


//get answer


if ()//test for answer correctness here


{


isAnswerCorrect = true;


}


if(askedQuestionCounter == 3)


{


keepAskingQuestion = false;


}


askedQuestionCounter++;


}


//here you will either have isAnswerCorrect = true or


//isAnswerCorrect = false


No comments:

Post a Comment