我在整个高中都参加过计算机编程课程,而现在,在我大四的时候,我们终于开始实际输入代码并制作程序了。我的课程基于C#而我们最近(过去的星期一)开始学习实际的代码而不是理论。
我试图超越曲线而且我已经获得了Console.WriteLine();并分配基本变量,整数等。基本的东西,我知道,但我们都从某个地方开始。我想要超越曲线,所以我决定开始研究一个非常简单的基于文本的互动故事,如果你愿意的话。它与战斗系统或统计数据无关。只需询问一个问题,并提供供用户选择的选项。
Console.WriteLine("Please enter your name: ");
string name = Console.ReadLine();
Console.Clear();
Console.Write("Hi " + name);
Console.WriteLine(", how are you?);
Console.WriteLine();
Console.WriteLine("1.) I'm well.");
Console.writeLine("2.) I'm not well.");
string areYouWell = Console.ReadLine();
if (areYouWell=="1")
{
Console.WriteLine("I'm Well.");
}
else if (areYouWell== "2")
{
Console.WriteLine("I'm not well.");
}
else
{
// this is where my question is. i want this else statement to
re-execute the Console.Write("Hi, " +name); line
我的问题是:如果用户输入未分配的值,我想让用户返回到代码块的开头,我该怎么做?我在那里谨慎地使用批量和goto功能,但在我对这个主题的研究中,我已经学会了goto基本上是第8个致命的罪。有什么替代方案?
这是我现在的代码:
namespace NameGame
{
class Jade
{
public static void Main(string[] args)
{
/*Tx-Y
* T= Tier x=tier level Y=option
*
*
*
* */
Start: Console.WriteLine("Hi, welcome to JADE. Im sure I could've come up with a clever acronymn.. but I didn't.");
Console.WriteLine("JADE is the project that I work on in my free time to practice programming. I hope you enjoy.");
Console.WriteLine();
Console.WriteLine("(At any time press \"Esc\" to close the program or type \"Ctrl\" to come back to this screen");
Console.WriteLine("What is your name?");
string usersName = Console.ReadLine();
Console.Clear();
Console.Write("It's nice to meet you " + usersName);
Console.WriteLine("! My name is Jade, a programmable coversationalist!");
Console.Write("I would LOVE to have a conversation with you! Afterall, it ");
Console.WriteLine("is what I was made for!");
Console.WriteLine("\n");
Console.WriteLine("So, what do you want to talk about?");
Console.WriteLine("\n");
Console.WriteLine("(Press a corresponding number on the keyboard and press enter)");
Console.WriteLine("1.) How your day is going");// start of tier one questions | Jade T1-O1
Console.WriteLine("2.) Relationships"); //Jade T1-O2 || For relationships use if statement to personalize based on a hand full of names or default to a simple message
//add more dialogue branches
string questionOne = Console.ReadLine(); //declaring string questionOne for the first question jade asks the user
if (questionOne == "1")
{
Console.Clear();
Console.WriteLine("OOO! I love talking about people's days! What have you done today?");
Console.WriteLine("\n");
Console.WriteLine("1.) School work mostly"); //Tier2-Question1-Option1
Console.WriteLine("2.) Took a test or quiz"); //T2-Q1-O2
Console.WriteLine("3.) Died on the inside due to school's neverending drag of despair."); //T2-O3
string T2Q1 = Console.ReadLine();
// continue this path
}
else if(questionOne=="2")
{
Console.Clear();
Console.WriteLine("OOO! I love a good romance! So, tell me. Is it good or bad?");
Console.WriteLine("\n");
Console.WriteLine("\n");
Console.WriteLine("1.) Good"); //T2-Q2-O1
Console.WriteLine("2.) Bad"); //T2-Q2-O2
string T2Q2 = Console.ReadLine();
//continue this path
}
else
{
return (if (questionOne == "1"));
}
Console.ReadKey();
}
}
}
对于这篇冗长的帖子感到抱歉,我已经找了一段时间了,现在大声笑了
答案 0 :(得分:0)
有很多不同的方法可以做到这一点。最容易理解的是do ... while
循环。
string questionOne;
do
{
Console.WriteLine("(Press a corresponding number on the keyboard and press enter)");
Console.WriteLine("1.) How your day is going");
Console.WriteLine("2.) Relationships");
questionOne = Console.ReadLine();
if (questionOne == "1")
{
// questions about how your day is going
}
else if (questionOne == "2")
{
// relationships questions
}
else
{
Console.WriteLine("I didn't understand your answer. Try again.");
}
} while (questionOne != "1" && questionOne != "2");
只要do ... while
不是“1”或“2”,questionOne
循环就会重复。因此,如果用户输入“3”,程序将输出“我不理解你的答案”,然后它会遇到while
语句。由于questionOne
既不是“1”也不是“2”,它将回到循环的开始。