2017-09-22 50 views
0

我正在進行一個小小的對話AI,我試圖讓它詢問用戶它們是怎麼樣的。我試圖通過刪除答覆中的「謝謝」一詞來縮小它的範圍,以減少可能性。因此,如果感謝詞在回覆中只是將其刪除,並且只需要尋找「好」或「好」的反應,而不是創建「好的」,「非常感謝」,「好的」偉大的」 我得到了消除字部分來自其他網站,但我正確使用它,我不認爲。 這裏是我到目前爲止的代碼。從字符串中刪除一個單詞C#

static void Main(string[] args) 
{ 
    Console.WriteLine("What is your name?"); 
    string name; 
    name = Console.ReadLine(); 
    Console.WriteLine("Oh hi {0}! Nice to meet you.", name); 

    Console.WriteLine("How are you doing today?"); 
    string mood; 
    mood = Console.ReadLine(); 
    mood = mood.ToLower(); 
    int index1 = mood.IndexOf("thanks"); 
    if (index1 != -1) 
    { 
     string mood2 = mood.Remove(index1); 
    } 
    else 
    { 

    } 
    if (mood2 == "good" || mood2== "alright" || mood2 == "great") 
    { 
     Console.WriteLine("Good to hear that!"); 
    } 
    else 
    { 
     Console.WriteLine("Ah well"); 
    } 
} 

感謝您的幫助。

+3

嘗試與string.replace – Mueui

+1

如果刪除(索引1)除其1個字符 - 你省略指出 - 嘗試刪除「所有」它雖然是@Mueui說有其他的選擇 – BugFinder

+6

'串mood2 = mood.Replace(「thanks」,「」);' –

回答

2

Remove方法的使用實際上是正確的它會產生假陽性按照documentation它。:

返回這在目前的實例中的所有角色,開始在指定的位置,並通過最後的位置繼續,已被刪除一個新的字符串。

你的代碼應該給你一個編譯器錯誤,在當前上下文中不存在變量mood2。無論是移動宣佈了IF-範圍:

string mood2 = ""; 
if (index1 != -1) 
{ 
    mood2 = mood.Remove(index1); 
} 

或簡單地覆蓋原來的mood變量,並將其用於進一步的比較:

if (index1 != -1) 
{ 
    mood = mood.Remove(index1); 
} 
else 
{ 

} 
if (mood == "good" || mood == "alright" || mood == "great") 
{ 
    Console.WriteLine("Good to hear that!"); 
} 
else 
{ 
    Console.WriteLine("Ah well"); 
} 

檢查的更簡單的方法是使用String.Contains方法。它會檢查你的字符串是否包含特定的單詞。所以,你可以跳過去除部分的簡單檢查是這樣的:

string mood = Console.ReadLine().ToLower(); 

if (mood.Contains("good") || mood.Contains("alright") || mood.Contains("great")) 
{ 
    Console.WriteLine("Good to hear that!"); 
} 
else 
{ 
    Console.WriteLine("Ah well"); 
} 

編輯:

如果你想檢查否定你可以做同樣的方式:

if (mood.Contains("good") || mood.Contains("alright") || mood.Contains("great")) 
{ 
    if(mood.Contains("not") 
    { 
     Console.WriteLine("Ah well"); 
    } 
    else 
    { 
     Console.WriteLine("Good to hear that!"); 
    } 
} 
else 
{ 
    Console.WriteLine("Ah well"); 
} 

相同你需要爲壞的描述,壞的描述,恐怖的描述等等。

+1

我希望你不介意我在我的回答中加了你的頭號部分(當然我給了信貸) – EpicKip

+0

@EpicKip那完全沒問題:) –

+0

嗨,感謝您的幫助。我用String.Contain試過了你的第二個方法,它工作的很好。然而,正如EpicKip所說,它不適用於誤報,就像我要說「不好」,因爲代碼只是在尋找「好」這個詞。我是否可以用同樣的方法去查找單詞not,如果字符串是一個肯定的單詞加上「not」這個單詞,它將返回「Ah well」,反之亦然,對於一個否定詞,單詞「not」? –

1

你可以刪除像你之前做的一個字,但該字符串沒有初始化(如Mong Zhu所述),所以你需要初始化它,然後它會工作:

string mood2 = "";// < Correct if this is added 
if (index1 != -1) 
{ 
    mood2 = mood.Remove(index1); 
} 

清潔方法:

但你確實需要刪除「謝謝」的一部分,因爲它站在你只檢查在該字符串還有什麼(mood2 == "good" || mood2== "alright" || mood2 == "great")你可以只檢查,如果您的字符串中包含這些詞彙之一,代碼會變得短了很多:「!相當不錯的」

string[] moods = { "good", "alright", "great" }; 
string name, chosenMood; 

Console.WriteLine("What's your name?"); 
name = Console.ReadLine(); 
Console.WriteLine("How are you doing today?"); 
chosenMood = Console.ReadLine(); 

if(moods.Any(mood => chosenMood.ToLower().Contains(mood))) 
{ 
    Console.WriteLine("Good to hear that!"); 
} 
else 
{ 
    Console.WriteLine("Ah well"); 
} 
Console.ReadKey(); 

這種方式,您也可以鍵入類似它仍然會工作

如果你回答像「也不好:(」

0

爲了您的目標,我只需檢查用戶的mood從您的「感覺良好」的單詞開始。

var goodAnswers = new string[] {"good", "alright", "great"}; 
if (goodAnswers.Any(okAnswer => mood.ToLower().StartsWith(okAnswer))) 
{ 
    Console.WriteLine("Good to hear that!"); 
} 
else 
{ 
    Console.WriteLine("Ah well"); 
}