2 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
我做了一些更改以使其正常工作。請(qǐng)參閱我在代碼中的注釋。
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貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
所以羅伯特提供了一個(gè)修復(fù)程序,但既然你說(shuō)你是編程新手,我將嘗試解釋這個(gè)問(wèn)題,并且我將嘗試用你正在做的事情的簡(jiǎn)化示例來(lái)實(shí)現(xiàn):
string text = "Hello!";
for(int i = 0; i < text.Length; i++)
{
text += "!";
}
您的循環(huán)條件基于 text.Length,但在循環(huán)內(nèi),您將附加到該文本,因此每次循環(huán)準(zhǔn)備再次開(kāi)始時(shí),它都會(huì)檢查 text.Length 并說(shuō):“好吧,我 - 仍然 - 不在 text.Length 的末尾,所以我想我會(huì)繼續(xù)循環(huán)。
如果您只想根據(jù)初始字符數(shù)進(jìn)行循環(huán),您應(yīng)該:
A. 在循環(huán)之前將 text.Length 存儲(chǔ)到單獨(dú)的變量中,然后將循環(huán)條件基于它,如下所示:
string text = "Hello!";
int initialLength = text.Length;
for(int i = 0; i < initialLength; i++)
{
text += "!";
}
B. 或者更好的是,不要將更多內(nèi)容附加到文本變量上,而是使用單獨(dú)的字符串變量作為“修改后的”副本:
string text = "Hello!";
string text2 = text;
for(int i = 0; i < text.Length; i++)
{
text2 += "!";
}
C. 或者最好采用方法 B,但使用 StringBuilder 類(lèi)。在 C# 中,當(dāng)您修改字符串時(shí),系統(tǒng)會(huì)在內(nèi)存中創(chuàng)建字符串的全新副本以及額外的字符,因此:
text = "Hello";
text += "!";
text += "!";
text += "!";
...實(shí)際上是在內(nèi)存中創(chuàng)建四個(gè)不同的字符串變量:
你好你好!你好!!你好?。。?/p>
這是暫時(shí)的事情,但效率低且浪費(fèi)。StringBuilder 類(lèi)旨在允許您逐段高效地構(gòu)建字符串:
StringBuilder sb = new StringBuilder("Hello");
sb.Append("!");
sb.Append("!");
sb.Append("!");
Console.WriteLine(sb.ToString()); // "Hello!!!"
因此,如果您查看示例 B,您可能會(huì)將其更改為:
string text = "Hello!";
StringBuilder sbText = new StringBuilder(text);
for(int i = 0; i < text.Length; i++)
{
sbText.Append("!");
}
我的最終建議是也按照您想要的方式獲取字符串/內(nèi)容,然后將其分配給您的控件。所以不要調(diào)用這個(gè) 10 次:
plTxtBx.Text += text;
相反,以正確的方式設(shè)置文本,然后在最后,只需做一個(gè)簡(jiǎn)單的分配:
plTxtBx.Text = final_text;
原因是控件具有各種事件以及當(dāng)您修改文本等屬性時(shí)運(yùn)行的小齒輪和齒輪。如果您使用 += 技巧將文本附加到 Textbox 控件,那么每次更新文本時(shí)都會(huì)強(qiáng)制該控件運(yùn)行其整個(gè)例程。
這些都是很小的低效率問(wèn)題,但隨著時(shí)間的推移,它們會(huì)逐漸增加,因此最好提前做好這些工作。
因此,如果我采用羅伯特提出的代碼(我沒(méi)有親自測(cè)試過(guò))并對(duì)其進(jìn)行一些更改,我會(huì)這樣做:
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 關(guān)注
- 154 瀏覽
添加回答
舉報(bào)