In: Computer Science
Please code in c# (C-Sharp)
Write a program that will ask the user for their name. If the user does not input anything, display a
warning before continuing. The program will then ask the user whether they want to have an addition,
subtraction, multiplication, or division problem. Once the user indicates their choice, the program will
display 2 randomly generated numbers from 1 to 9 in a math problem matching the user’s choice.
Example: user selects addition, the equation presented is:
(random number from 1 to 9) + (random number from 1 to 9)
The user will then input an answer as a whole number. If the answer they entered is not a whole
number, tell the user their input is invalid and end the program. If the input is valid, check to see if it is
the correct answer. If correct, congratulate the user by name. If incorrect, output the correct answer.
Tasks
1) The program needs to contain the following
a.
A comment header containing your name and a brief description of the program
b. At least 5 comments besides the comment header explaining what your code does
c.
A string variable that captures the user’s name
d. A way to validate user input to determine if they entered an empty string
i. Warn the user if they enter an empty string
ii. Continue the program after warning the user
e. A prompt for what type of quiz the user wants
f.
Display of a quiz matching the type the user requested
g.
A prompt for an answer in the form of a whole number
i. A message if the user enters an invalid number
1. Do not check the user’s answer if their input is invalid
ii. If input is valid:
1. Check the answer
2. If the answer is correct, congratulate the user by name
3. If incorrect, output the correct answer
h. “Press enter to continue” and Console.ReadLine(); at the end of your code
i. Note: if you write a try-catch statement, these lines will be after your last catch
2) Upload a completed .cs file onto the Assignment 4 submission folder and a word document
containing the following six (6) screenshots:
a.
The warning displayed when the user enters a blank value for the name
b. One test run for each equation with valid input
i. Answer 2 quizzes correctly
ii. Answer 2 quizzes incorrectly
c.
One test run with invalid input
//THE CODE IS PROVIDED BELOW
//PASTING IT ONLINE CAUSES SOME INVALID CHARACTERS TO GET
INTRODUCED IN THE CODE.
//HENCE, PLEASE GO THROUGH THE SCREENSHOT BELOW FOR INDENTATION AND
OTHER REFERENCE.
using System;
public class Test
{
public static void Main()
{
//Declare Variables
string name,answer_string,option_string;
Random random_generator = new Random();
int option_integer=0,a,b,answer_integer=0;
//Input Name
Console.Write("Enter your name: ");
name = Console.ReadLine();
//Show warning if name is left empty
if(String.IsNullOrEmpty(name))
Console.WriteLine("\nWarning: No name provided.");
//Give options
Console.WriteLine("Enter 1 for addition");
Console.WriteLine("Enter 2 for subtraction");
Console.WriteLine("Enter 3 for multiplication");
Console.WriteLine("Enter 4 for division");
//Use try catch to check for integers
try
{
option_string = Console.ReadLine();
option_integer = Int32.Parse(option_string);
}
catch (FormatException e)
{
Console.WriteLine("Invalid Format");
return;
}
if(option_integer == 1) //Addition is selected
{
//generate the two random numbers
a=random_generator.Next(0,9);
b=random_generator.Next(0,9);
Console.WriteLine("{0} + {1}",a,b); //Print out the
equation
//Give warning to user to input only Whole Numbers
Console.WriteLine("Please ONLY enter whole number as the
answer");
Console.WriteLine("Do not round off, just write the whole number
part of your answer");
try
{
//Check for whole number using try catch
answer_string=Console.ReadLine();
answer_integer=Int32.Parse(answer_string);
}
catch (FormatException e)
{
//Print the error message and exit the program
Console.WriteLine("Invalid Format");
return;
}
//if input is correct, check the answer
if(answer_integer==(a+b))
Console.WriteLine("Congrats {0}. Your answer is
correct!",name);
else
Console.WriteLine("Wrong Answer. The correct answer is
{0}.",(a+b));
}
if(option_integer == 2) //Subtraction is selected
{
//generate the two random numbers
a=random_generator.Next(0,9);
b=random_generator.Next(0,9);
//check for the larger number. This is useful in subtraction
mostly and also slightly for division
if(b>a)
{
int temp=a;
a=b;
b=temp;
}
Console.WriteLine("{0} - {1}",a,b); //Print out the
equation
//Give warning to user to input only Whole Numbers
Console.WriteLine("Please ONLY enter whole number as the
answer");
Console.WriteLine("Do not round off, just write the whole number
part of your answer");
try
{
//Check for whole number using try catch
answer_string=Console.ReadLine();
answer_integer=Int32.Parse(answer_string);
}
catch (FormatException e)
{
//Print the error message and exit the program
Console.WriteLine("Invalid Format");
return;
}
//if input is correct, check the answer
if(answer_integer==(a-b))
Console.WriteLine("Congrats {0}. Your answer is
correct!",name);
else
Console.WriteLine("Wrong Answer. The correct answer is
{0}.",(a-b));
}
if(option_integer == 3) //Multiplication is selected
{
//generate the two random numbers
a=random_generator.Next(0,9);
b=random_generator.Next(0,9);
Console.WriteLine("{0} * {1}",a,b); //Print out the
equation
//Give warning to user to input only Whole Numbers
Console.WriteLine("Please ONLY enter whole number as the
answer");
Console.WriteLine("Do not round off, just write the whole number
part of your answer");
try
{
//Check for whole number using try catch
answer_string=Console.ReadLine();
answer_integer=Int32.Parse(answer_string);
}
catch (FormatException e)
{
//Print the error message and exit the program
Console.WriteLine("Invalid Format");
return;
}
//if input is correct, check the answer
if(answer_integer==(a*b))
Console.WriteLine("Congrats {0}. Your answer is
correct!",name);
else
Console.WriteLine("Wrong Answer. The correct answer is
{0}.",(a*b));
}
if(option_integer == 4) //Division is selected
{
//generate the two random numbers
a=random_generator.Next(0,9);
b=random_generator.Next(0,9);
//check for the larger number. This is useful in subtraction
mostly and also slightly for division
if(b>a)
{
int temp=a;
a=b;
b=temp;
}
if(b==0)
b=1; //since we can not divide by 0, we handle this by making it
1
Console.WriteLine("{0} / {1}",a,b); //Print out the equation
//Give warning to user to input only Whole Numbers
Console.WriteLine("Please ONLY enter whole number as the
answer");
Console.WriteLine("Do not round off, just write the whole number
part of your answer");
try
{
//Check for whole number using try catch
answer_string=Console.ReadLine();
answer_integer=Int32.Parse(answer_string);
}
catch (FormatException e)
{
//Print the error message and exit the program
Console.WriteLine("Invalid Format");
return;
}
//if input is correct, check the answer
if(answer_integer==(a/b))
Console.WriteLine("Congrats {0}. Your answer is
correct!",name);
else
Console.WriteLine("Wrong Answer. The correct answer is
{0}.",(a/b));
}
if(option_integer < 1)//Invalid Option
{
Console.WriteLine("Invalid option");
return;
}
if(option_integer > 4)//Invalid Option
{
Console.WriteLine("Invalid option");
return;
}
Console.WriteLine("Press enter to continue");
Console.ReadLine();
}
}
//Screenshots of the code:
//Screenshot of blank value:
//Screenshots of correct answers:
//Screenshots of incorrect answers:
//Screenshots of invalid input: