In: Computer Science
Assignment Description
Write a program that will have a user guess whether a person is a musician or a writer. You will create a
group of four names, two musicians and two writers, and show the user a name randomly selected from
the four. The user will then guess whether they think the name belongs to a musician or writer. After a
guess, the program will tell the user whether they are correct, repeat the name, and reveal 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.
Four string variables, two musicians and two writers
d. A way to randomly display one of the four names to the user
e. Output asking the user whether the name displayed is a musician or a writer
i. If the user guesses correctly, congratulate them before outputting the displayed
name and the correct answer
ii. If the user guesses incorrectly, output the displayed name and the correct
answer
f.
“Press enter to continue” and Console.Read(); at the end of your code
2) Upload the completed .cs file onto the Assignment 3 submission folder. To access the .cs file:
a.
Right click on your program tab
b. Click “Open containing folder”
c.
The file you are working on will be there with a .cs extention, upload the .cs file
Answer:
using System;
namespace ConsoleApp1
{
class Program
{
static void
Main(string[] args)
{
string[] selectNames = { "Alex", "Ryan", "Alice", "Rebecca" };
//array to store the names
string[] selectProfession = { "Musician", "Writer" }; //array to
store the profession
string Name = null;
string Profession=null;
string readInput;
Random r = new Random(); //Random class is used to generate a name and profession randomly
int index = r.Next(selectNames.Length); //stores the index of the
randomly selected name
int index2 = r.Next(selectProfession.Length); //stores the index of
the randomly selected profession
Name = selectNames[index]; //stores the random name in a name variable of type string
//checks for each name in array and assigns a random profession to
each of them
if (Name == "Alex")
Profession = selectProfession[index2];
else if (Name == "Ryan")
Profession = selectProfession[index2];
else if (Name == "Alice")
Profession = selectProfession[index2];
else if (Name == "Rebecca")
Profession = selectProfession[index2];
//Displays a name randomly to the user and asks them guess the
person's profession
Console.WriteLine("Guess whether the following person is musician
or writer:");
Console.WriteLine("Name: " + Name + "\n");
Console.WriteLine("User's Turn ");
Console.WriteLine("Guess whether "+ Name + " is a musician or
writer:");
readInput = Console.ReadLine(); //stores the userInput in a string
variable
//if user input is equal to the profession of the person then you
win else you lose
if (readInput.Equals(Profession))
{
Console.WriteLine("Congratulations...!! \nYou have guessed the
profession correctly");
Console.WriteLine("Name: " + Name + "\nProfession: " +
Profession);
}
else
{
Console.WriteLine("Uh oh...!! \nYour guess was incorrect\nCorrect
Guess is:");
Console.WriteLine("Name: " + Name + "\nProfession: " +
Profession);
}
Console.WriteLine("\n\nPress Enter to continue");
Console.Read();
}
}
}
Output: