Assignment is to create a magic 8 ball program. I have set up a random object and established all that. However, part of the assignment is for the program to continue until the user enters nothing into the console, and then it will exit the program.
I've tried using a control variable of a blank string "" to exit the console upon that entry, but I'm not sure what I'm missing. Anything I do in the MagicPCUI class hasn't worked yet so I'm not going to include those changes into the code, just the basic stuff I have set up already. But here is my main:
internal class Dalzell_MagicPCApp
{
static void Main(string[] args)
{
Info info = new Info("LastName_MagicPCApp");
MagicPCUI ui = new MagicPCUI();
ui.UserInstructions();
ui.Start();
Console.ReadKey();
}
}
Here is my MagicPCUI class. This is where I'm getting confused about where/which loop to include.
internal class MagicPCUI
{
//field userMagic for MagicPC
string userMagic;
//instructions for display in console
public void UserInstructions()
{
WriteLine("******************************************************************************");
WriteLine("\n\n\tWelcome my friend welcome. Come closer. That's right.");
WriteLine("\n\n\tThis is the magic PC screen. All your questions will be answered!");
WriteLine("\tJust enter a question and the magic PC screen will give you the answer!");
WriteLine("\n\n\tHowever, it must be a question that can be answered with yes or no.");
WriteLine("\tTo exit the program, just hit the Enter key.");
WriteLine("\n\n******************************************************************************");
WriteLine("\n\nHit any key to continue.");
Console.ReadKey();
Console.Clear();
}
//starts the game. prompts the user to enter a question and calls
//RespontToQuestion method for a randomized response.
public void Start()
{
GetUserQuestion();
RespondToQuestion();
WriteLine("Until we meet again, may you have good fortune.");
}
//reads the input from the user and returns the string
public string GetUserQuestion()
{
WriteLine("Enter your question, if you dare...");
string userQuestion = Console.ReadLine();
return userQuestion;
}
//instantiates the magicPC object and pulls a random response
public void RespondToQuestion()
{
MagicPC magicPC = new MagicPC();
magicPC.GetUserAnswer();
Console.ReadKey();
Console.Clear();
}
}
And not sure if this is necessary to include but here's the MagicPC class for the randomized responses:
internal class MagicPC
{
//generates random number and deteremines response to user question
public string GetUserAnswer()
{
Random random = new Random();
int randomAnswer;
randomAnswer = random.Next(0, 8);
switch (randomAnswer)
{
case 0: WriteLine("\nYes.");
break;
case 1: WriteLine("\nNo.");
break;
case 2: WriteLine("\nIt does not appear likely.");
break;
case 3: WriteLine("\nAll signs point to yes.");
break;
case 4: WriteLine("\nThe answer is cloudy, please try again.");
break;
case 5: WriteLine("\nYou should already know the answer to that!");
break;
case 6: WriteLine("\nWhat a silly question.");
break;
default: WriteLine("\nOf course, duh!");
break;
}
string answer = randomAnswer.ToString();
return answer;
}
}
CodePudding user response:
You want to loop over question & answer pairs, until the question is blank:
public void Start()
{
while(GetUserQuestion() != ""){
RespondToQuestion();
}
WriteLine("Until we meet again, may you have good fortune.");
}
CodePudding user response:
Thank you! This was fairly obvious but just went over my head. Super appreciated!
public void Start()
{
while(GetUserQuestion() != "")
{
RespondToQuestion();
}
Console.Clear();
WriteLine("Until we meet again, may you have good fortune.");
}