Question

In: Computer Science

Please code in c# (C-Sharp) Write a program that will ask the user for their name....

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

Solutions

Expert Solution

//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:


Related Solutions

Please code in C# - (C - Sharp) Assignment Description Write out a program that will...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will ask the user for their name; the length and width of a rectangle; and the length of a square. The program will then output the input name; the area and perimeter of a rectangle with the dimensions they input; and the area and perimeter of a square with the length they input. Tasks The program needs to contain the following A comment header containing...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If...
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If the age is greater than 21 print "welcome," and if the age is less than 21 print "sorry." Use input validation to make sure the birthdate was entered correctly.
c++ In this program ask the user what name they prefer for their file, and make...
c++ In this program ask the user what name they prefer for their file, and make a file, the file name should za file. Get a number from the user and save it into the create file. should be more than 20 number in any order. print out all 20 numbers in a sorted array by using for loop, last print out its total and average. At last create a function in which you will call it from main and...
​​​​​​​in c code Add an intro screen Ask the user for their name, what type of...
​​​​​​​in c code Add an intro screen Ask the user for their name, what type of cookie they would like to order and how many. The types are sugar, chocolate chip, and peanut butter. Assign a cost to each cookie. Show total for cookie purchase and ask if the would like to place another order. Include at least one function and one loop. Add one extra feature. For example, a sample execution of your code would be as follows: Cookie...
Please code C# 8. Write a program that prompts the user to enter an integer. The...
Please code C# 8. Write a program that prompts the user to enter an integer. The program then determines and displays the following: Whether the integer is divisible by 5 and 6 Whether the integer is divisible by 5 or 6
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Please code C# 10. Write a program that allows a user to input names and corresponding...
Please code C# 10. Write a program that allows a user to input names and corresponding heights (assumed to be in inches). The user can enter an indefinite number of names and heights. After each entry, prompt the user whether they want to continue. If the user enters true, ask for the next name and height. If the user enters false, display the name of the tallest individual and their height. Sample run: “Name?” James “Height?” 50 “Continue?” True “Name?”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT