任務是創建一個魔術 8 球程式。我已經設定了一個隨機物件并建立了所有這些。但是,部分任務是讓程式繼續運行,直到用戶在控制臺中沒有輸入任何內容,然后它將退出程式。
我嘗試使用空白字串“”的控制變數在該條目時退出控制臺,但我不確定我錯過了什么。我在 MagicPCUI 類中所做的任何事情都還沒有起作用,所以我不會將這些更改包含到代碼中,只是我已經設定的基本內容。但這是我的主要內容:
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();
}
}
這是我的 MagicPCUI 課程。這是我對要包含的位置/哪個回圈感到困惑的地方。
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();
}
}
我不確定這是否有必要包括在內,但這里是隨機回應的 MagicPC 類:
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;
}
}
uj5u.com熱心網友回復:
您想回圈問題和答案對,直到問題為空白:
public void Start()
{
while(GetUserQuestion() != ""){
RespondToQuestion();
}
WriteLine("Until we meet again, may you have good fortune.");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/429170.html
