|
-
November 19th, 2011, 01:06 PM
#1
[RESOLVED] C# Help: I can't find my coding error... :(
The issues Ive noticed is that the score is not increasing properly... it goes up by 3, when it should be increasing by 10.
Computers have been widely used to test students on their ability to solve arithmetic problems. Write a program that will test a student on addition, subtraction, or multiplication using random integers between 1 and 100. The student begins by choosing the type of problem and then is asked 10 problems with 3 chances to answer each correctly. If after 3 chances the answer is still incorrect, the correct answer is displayed. A score is calculated by awarding 10 points for a correct answer on the first try, 5 points on the second try, 3 points on the third try, and 0 points if all three attempts are wrong. The program output may look similar to:
Enter the number for the problem type desired.
1. Addition
2. Subtraction
3. Multiplication
Enter choice: 1
25 + 73 = 79
WRONG Try again.
25 + 73 = 98
CORRECT
12 + 94 = 106
CORRECT
15 + 6 = 19
WRONG Try again.
15 + 6 = 18
WRONG Try again.
15 + 6 = 20
You have missed 3 times. The answer is 21.
36 + 49 = 85
CORRECT
…
17 + 65 = 82
CORRECT
Your score = 70
class Program
{
static Random randy = new Random();
static int score;
static int chances;
static void ScoreSolver(int guesses)
{
if (guesses == 1)
{
score += 10;
Console.WriteLine("+10 ... Your score is " + score);
}
if(guesses == 2)
{
score += 5;
Console.WriteLine("+5 ... Your score is " + score);
}
if (guesses == 3)
{
score += 3;
Console.WriteLine("+3 ... Your score is " + score);
}
if (guesses > 3)
{
Console.WriteLine("+0 ... Your score is " + score);
}
}
static void Addition(int number1, int number2)
{
string question = "";
int answer = 0;
for (int loopy = 1; loopy <= 10; loopy++)
{
chances = 1;
number1 = randy.Next(1, 101);
number2 = randy.Next(1, 101);
question = "\n" + number1 + " + " + number2 + " = ";
Console.Write(question);
answer = int.Parse(Console.ReadLine());
do
{
if (answer != number1 + number2)
{
Console.WriteLine("Wrong Try Again.");
Console.Write(question);
answer = int.Parse(Console.ReadLine());
}
chances++;
} while (chances < 3);
if (answer == number1 + number2)
{
Console.WriteLine("\nCORRECT!");
}
else if (chances == 3)
{
Console.WriteLine("You missed 3 Times. The answer is " + (number1 + number2));
chances++;
}
ScoreSolver(chances);
}
}
static void Subtraction(int number1, int number2)
{
string question = "";
int answer = 0;
for (int loopy = 1; loopy <= 10; loopy++)
{
chances = 1;
number1 = randy.Next(1, 101);
number2 = randy.Next(1, 101);
question = "\n" + number1 + " - " + number2 + " = ";
Console.Write(question);
answer = int.Parse(Console.ReadLine());
do
{
chances++;
if (answer != number1 - number2)
{
Console.WriteLine("Wrong Try Again.");
Console.Write(question);
answer = int.Parse(Console.ReadLine());
}
} while (chances < 3);
if (answer == number1 - number2)
{
Console.WriteLine("\nCORRECT!");
}
else if (chances == 3)
{
Console.WriteLine("You missed 3 Times. The answer is " + (number1 - number2));
chances++;
}
ScoreSolver(chances);
}
}
static void Multiplication(int number1, int number2)
{
string question = "";
int answer = 0;
for (int loopy = 1; loopy <= 10; loopy++)
{
chances = 1;
number1 = randy.Next(1, 101);
number2 = randy.Next(1, 101);
question = "\n" + number1 + " x " + number2 + " = ";
Console.Write(question);
answer = int.Parse(Console.ReadLine());
do
{
if (answer != number1 * number2)
{
Console.WriteLine("Wrong Try Again.");
Console.Write(question);
answer = int.Parse(Console.ReadLine());
}
chances++;
} while (chances < 3);
if (answer == number1 * number2)
{
Console.WriteLine("\nCORRECT!");
}
else if (chances == 3)
{
Console.WriteLine("You missed 3 Times. The answer is " + number1 * number2);
chances++;
}
ScoreSolver(chances);
}
}
static void StartUp()
{
int problemType = -1;
do
{
Console.Write("\nEnter the number for the problem type desired.");
Console.Write("\n\t1. Addition \n\t2. Subtraction \n\t3. Multiplication \nDesired Type: ");
//problemType = int.Parse(Console.ReadLine());
do
{
try
{
problemType = int.Parse(Console.ReadLine());
}
catch (Exception e)
{
Console.Write("\nProgram threw an exception: ");
Console.Write(e.Message);
Console.Write("\nChoose Again: ");
}
} while (problemType == -1);
} while (problemType < 1 || problemType > 4);
if (problemType == 1)
{
Addition(randy.Next(1, 101), randy.Next(1, 101));
}
if (problemType == 2)
{
Subtraction(randy.Next(1, 101), randy.Next(1, 101));
}
if (problemType == 3)
{
Multiplication(randy.Next(1, 101), randy.Next(1, 101));
}
}
static void Main(string[] args)
{
StartUp();
Console.WriteLine("Thanks for playing");
Console.ReadKey(true);
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|