我真的不知道如何正確表達這個問題,因為我對此非常陌生,所以這里是:
我正在嘗試創建一個“自己冒險”,每個問題只有兩個答案選項。起初,我打算使用數十個嵌套的 if 陳述句使其完全程式化,但建議我改用 OOP。這絕對是我的第一個 OOP 專案,所以我對我應該制作物件的內容感到非常無能為力。到目前為止,我唯一的想法是從“給出一段文字,然后詢問答案”的情況中創建一個物件。我還在 if 陳述句中提出了一個想法,我雖然是天才,但顯然沒有用,我嘗試使用 alt 引數作為下一個要運行的任務的物件名稱。
namespace Historia
{
public class Story
{
string text;
public void Quest (string text, object alt1, object alt2)
{
Console.WriteLine(text);
string input = Console.ReadLine();
if (input == "1")
{
alt1.Quest();
}
else if (input == "2")
{
alt2.Quest();
}
}
}
}
另一個頁面包含以下內容:
namespace Historia
{
class Program
{
static void Main(string[] args)
{
Story A1 = new Story();
Story A2 = new Story();
Story A3 = new Story();
A1.Quest("Welcome, are you there?", A2, A3);
}
}
}
同樣,我對如何取得進展感到非常無能為力,并希望得到一些意見。
uj5u.com熱心網友回復:
在您的Quest方法中,您將alt1and宣告alt2為object,因此它是一個匿名物件,您的代碼不會知道它是 Story 物件。
您的屬性string text;未被使用,因為在您的任務中您使用了 astring text并且它是另一個引數。
當您在 OOP 上編碼時,您通常會創建一個建構式*,正如您將在下面的代碼中看到的,當您像Story A1 = new Story("Welcome, are you there?");插入文本一樣初始化類時。
如您所見,我將輸入引數分配給類屬性,以便稍后在您呼叫A1.Quest(answer). A1 已經有故事文本。
namespace Historia
{
public class Story
{
string text_alt1;
string text_alt2;
// Constructor
public Story (string text_alt1, string text_alt2 = null)
{
this.text_alt1 = text_alt1;
this.text_alt2 = text_alt2;
}
// Method
public string Quest(string answer = null)
{
// If there is no answer because it is the first question
// show text1
if (answer == "1" || answer == null)
{
Console.WriteLine(text_alt1);
}
else if (answer == "2")
{
Console.WriteLine(text_alt2);
}
//Return the user input
return Console.ReadLine();
}
}
}
請注意上面代碼中的以下行代碼以進行其余說明: 我們將“void”** 更改為“string”,它是將回傳的值的型別。
public string Quest(string answer = null)
以及我們回傳用戶輸入的下一個代碼
return Console.ReadLine();
在另一個檔案中,我們首先使用建構式用故事文本宣告所有故事。
然后,我們初始化任務。當我們放置退貨時,它將為我們提供用戶輸入,因此我們將其存盤在 answer 并將其放入下一個任務中,然后我們再次存盤在 asnwer 中,覆寫上一個值等等。
還可以將所有這些物件添加到串列中并像回圈一樣對其進行迭代,這樣您就不需要一直撰寫answer = AX.Quest(answer);,只需撰寫一次。- 如果你想要我升級這個答案就告訴我,但我不想寫太多。
namespace Historia
{
class Program
{
static void Main(string[] args)
{
// Declaring stories
Story A1 = new Story("Welcome, are you there?");
Story A2 = new Story("Alternative1 A2", "Alternative2 A2");
Story A3 = new Story("Alternative1 A3", "Alternative2 A3");
//User interacting with stories
var answer = A1.Quest();
answer = A2.Quest(answer);
answer = A3.Quest(answer);
//...
}
}
}
如果您有更多問題,請提問 =)。
注意: 這個例子不包括如果在A1中你選擇1,在A2中你可以在A4、A5之間選擇,如果你在A3中選擇2你可以在A6、A7之間選擇。它是直系的,每個答案有 2 個。這意味著我在 A1 1 中選擇,所以在 A2 中我有 A4 和 A5,如果我在 A3 中選擇 A1 2,我有 A4 和 A5 可供選擇。
*Constructors are methods declared with the same name of the class, there can be multiple constructors in a single class but with different input parameters and usually you set properties with input parameters of the constructor to know the difference to properties you must write this.PropertyName which is needed if a property and a parameter has the same name.
string text_alt1;
string text_alt2;
// Constructor 1
public Story (string text_alt1, string text_alt2 = null)
{
this.text_alt1 = text_alt1;
this.text_alt2 = text_alt2;
}
// Constructor 2
public Story(string text_alt1, string text_alt2, string text_alt3 = null)
{
this.text_alt1 = text_alt1;
this.text_alt2 = text_alt2;
}
**Void it means that a method will not return any value.
EDIT increased answer after comment
Keeping in mind what you tell on your comment, this aproach should do the work.
Basically a constructor where you put your Story objects instead of anonymous and then a Quest() without input parameters.
public class Historia
{
public string text;
public Story story1;
public Story story2;
// Constructor 2
public Story(string text, Story story1 = null, Story story2 = null)
{
this.text = text;
this.story1 = story1;
this.story2 = story2;
}
public void Quest()
{
Console.WriteLine(text);
string input = Console.ReadLine();
if (input == "1" && story1 != null)
{
story1.Quest();
}
else if (input == "2" && story2 != null)
{
story2.Quest();
}
}
}
And on the main file, you define the text for each story and then you decide which story is connected with each other.
class Program
{
static void Main(string[] args)
{
Story A1 = new Story("Welcome, are you there?");
Story A2 = new Story("Alternative A2");
Story A3 = new Story("Alternative A3");
Story A4 = new Story("A2 with alternative A4");
Story A5 = new Story("A2 with alternative A5");
Story A6 = new Story("A3 with alternative A6");
Story A7 = new Story("A3 with alternative A7");
Story End = new Story("Concurrency for an specific time of the story or Ending");
Story timeTravel = new Story("TimeTraveling if you choose 2 at end", A2, A3);
//Declaring which stories are connected with other.
A1.story1 = A2;
A1.story2 = A3;
A2.story1 = A4;
A2.story2 = A5;
A3.story1 = A6;
A3.story2 = A7;
//Possible to do a connect again even if the stories were different and then split again
A4.story1 = End;
A5.story1 = End;
A6.story1 = End;
A7.story1 = End;
End.story2 = timeTravel;
A1.Quest();
}
}
如果它解決了您的問題,請投票并檢查答案會很好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/361543.html
標籤:C#
上一篇:從C#中的屬性獲取影像URL
