2 回答

TA貢獻1827條經驗 獲得超8個贊
我做了一些更改以使其正常工作。請參閱我在代碼中的注釋。
private void transBtn_Click(object sender, EventArgs e)
{
english = engTxtBx.Text;
english = english.Trim();
string[] columns = english.Split(' ');
for (int i = 0; i < columns.Length; i++)
{
if (isVowel(columns[i][0]))
{
// Start with vowel.
pigLatin = columns[i] + "way";
}
else
{
// Start with consonant. Get index of first vowel.
int index = columns[i].IndexOfAny(vowels);
if (index == -1)
{
// No vowel in columns[i].
// You have to decide what to do.
}
else if (index == 1)
{
// First vowel is the second letter.
pigLatin = columns[i].Substring(1) + columns[i][0] + "way";
}
else
{
// First vowel is after the second letter.
pigLatin = columns[i].Substring(index) + columns[i].Substring(index - 1, 1) + "way";
}
}
plTxtBx.Text += pigLatin;
}
}
private static char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
private static bool isVowel(char c)
{
return vowels.Contains(c);
}

TA貢獻1831條經驗 獲得超4個贊
所以羅伯特提供了一個修復程序,但既然你說你是編程新手,我將嘗試解釋這個問題,并且我將嘗試用你正在做的事情的簡化示例來實現:
string text = "Hello!";
for(int i = 0; i < text.Length; i++)
{
text += "!";
}
您的循環條件基于 text.Length,但在循環內,您將附加到該文本,因此每次循環準備再次開始時,它都會檢查 text.Length 并說:“好吧,我 - 仍然 - 不在 text.Length 的末尾,所以我想我會繼續循環。
如果您只想根據初始字符數進行循環,您應該:
A. 在循環之前將 text.Length 存儲到單獨的變量中,然后將循環條件基于它,如下所示:
string text = "Hello!";
int initialLength = text.Length;
for(int i = 0; i < initialLength; i++)
{
text += "!";
}
B. 或者更好的是,不要將更多內容附加到文本變量上,而是使用單獨的字符串變量作為“修改后的”副本:
string text = "Hello!";
string text2 = text;
for(int i = 0; i < text.Length; i++)
{
text2 += "!";
}
C. 或者最好采用方法 B,但使用 StringBuilder 類。在 C# 中,當您修改字符串時,系統會在內存中創建字符串的全新副本以及額外的字符,因此:
text = "Hello";
text += "!";
text += "!";
text += "!";
...實際上是在內存中創建四個不同的字符串變量:
你好你好!你好??!你好?。?!
這是暫時的事情,但效率低且浪費。StringBuilder 類旨在允許您逐段高效地構建字符串:
StringBuilder sb = new StringBuilder("Hello");
sb.Append("!");
sb.Append("!");
sb.Append("!");
Console.WriteLine(sb.ToString()); // "Hello!!!"
因此,如果您查看示例 B,您可能會將其更改為:
string text = "Hello!";
StringBuilder sbText = new StringBuilder(text);
for(int i = 0; i < text.Length; i++)
{
sbText.Append("!");
}
我的最終建議是也按照您想要的方式獲取字符串/內容,然后將其分配給您的控件。所以不要調用這個 10 次:
plTxtBx.Text += text;
相反,以正確的方式設置文本,然后在最后,只需做一個簡單的分配:
plTxtBx.Text = final_text;
原因是控件具有各種事件以及當您修改文本等屬性時運行的小齒輪和齒輪。如果您使用 += 技巧將文本附加到 Textbox 控件,那么每次更新文本時都會強制該控件運行其整個例程。
這些都是很小的低效率問題,但隨著時間的推移,它們會逐漸增加,因此最好提前做好這些工作。
因此,如果我采用羅伯特提出的代碼(我沒有親自測試過)并對其進行一些更改,我會這樣做:
private void transBtn_Click(object sender, EventArgs e)
{
// Start with whatever is in plTxtBx
StringBuilder sb = new StringBuilder(plTxtBx.Text);
// Break into words
string[] columns = engTxtBx.Text.Trim().Split(' ');
for (int i = 0; i < columns.Length; i++)
{
if (isVowel(columns[i][0]))
{
// Start with vowel.
sb.Append(columns[i]);
sb.Append("way");
}
else
{
// Start with consonant. Get index of first vowel.
int index = columns[i].IndexOfAny(vowels);
if (index == -1)
{
// No vowel in columns[i].
// You have to decide what to do.
}
else if (index == 1)
{
// First vowel is the second letter.
sb.Append(columns[i].Substring(1));
sb.Append(columns[i][0]);
sb.Append("way");
}
else
{
// First vowel is after the second letter.
sb.Append(columns[i].Substring(index));
sb.Append(columns[i].Substring(index - 1, 1));
sb.Append("way");
}
}
}
// Update plTxtBx's Text once with the final results
plTxtBx.Text = sb.ToString();
}
- 2 回答
- 0 關注
- 157 瀏覽
添加回答
舉報