gpt4 book ai didi

c# - Pig Latin Translator 吐出多行? C#

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:04 25 4
gpt4 key购买 nike

所以我有一个支持多个单词的 Pig Latin Translator。但是每当我输入一些单词时(例如,我们只使用“banana apple shears chocolate Theodore train”。)它会正确地吐出翻译的单词,但它会重复!这是我的代码:

namespace Pig_Latin_Translator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> vowels = new List<string>();
List<string> specials = new List<string>();

private void TranslateButton_Click(object sender, EventArgs e)
{
String[] parts = TranslateBox.Text.Split();
foreach (string s in specials)
{
if (TranslateBox.Text.Contains(s) || TranslateBox.Text == "\"")
{
TranslateOutput.Text = "";
MessageBox.Show("No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
break;
}
else
{
foreach (String part in parts)
{
foreach (String v in vowels)
{
if (part.Substring(0, 1) == v)
{
TranslateOutput.Text = TranslateOutput.Text + " " + part + "ay";
break;
}
else
{
if (part.Substring(0, 2) == "sh" || part.Substring(0, 2) == "ch" || part.Substring(0, 2) == "th" || part.Substring(0, 2) == "tr")
{
string SwitchP = part.Substring(2) + part.Substring(0, 2);
TranslateOutput.Text = TranslateOutput.Text + " " + SwitchP + "ay";
break;
}
else
{
string Switch = part.Substring(1) + part.Substring(0, 1);
TranslateOutput.Text = TranslateOutput.Text + " " + Switch + "ay";
break;
}
}
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
vowels.Add("a");
vowels.Add("e");
vowels.Add("i");
vowels.Add("o");
vowels.Add("u");

specials.Add("`");
specials.Add("1");
specials.Add("2");
specials.Add("3");
specials.Add("4");
specials.Add("5");
specials.Add("6");
specials.Add("7");
specials.Add("8");
specials.Add("9");
specials.Add("0");
specials.Add("-");
specials.Add("=");
specials.Add("[");
specials.Add("]");
specials.Add(@"\");
specials.Add(";");
specials.Add("'");
specials.Add(",");
specials.Add(".");
specials.Add("/");

specials.Add("~");
specials.Add("!");
specials.Add("@");
specials.Add("#");
specials.Add("$");
specials.Add("%");
specials.Add("^");
specials.Add("&");
specials.Add("*");
specials.Add("(");
specials.Add(")");
specials.Add("_");
specials.Add("+");
specials.Add("{");
specials.Add("}");
specials.Add("|");
specials.Add(":");
specials.Add("\"");
specials.Add("<");
specials.Add(">");
specials.Add("?");
}

private void AboutButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Pig Latin is a fake language. It works by taking the first letter (Or two if it's a pair like 'th' or 'ch') and bringing it to the end, unless the first letter is a vowel. Then add 'ay' to the end. So 'bus' becomes 'usbay', 'thank' becomes 'ankthay' and 'apple' becomes 'appleay'.", "About:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

如果您输入“banana apple shears chocolate Theodore train”,输出:

“ananabay appleay earshay ocolatechay heodoreTay aintray”重复 10 次以上。

顺便说一句:如果您不能回答,我很抱歉,因为我知道有很多代码。不过没关系,因为这东西还是有用的。只是它不应该发生,让我心烦意乱。我知道还有很多问题,还有很多事情要做,但我想先解决这个问题。

最佳答案

您将代码嵌套在不应嵌套的两个循环中

foreach (string s in specials)

foreach (String v in vowels)

您的 break 语句使您摆脱了其中一个的麻烦,但另一个却没有。

如果使用 .Any(...) 谓词,您可以完全避免这些循环。

您的代码可能如下所示:

private void TranslateButton_Click(object sender, EventArgs e)
{
TranslateOutput.Text = "";
if (specials.Any(s => TranslateBox.Text.Contains(s)))
{
MessageBox.Show("No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
String[] parts = TranslateBox.Text.Split();
foreach (var part in parts)
{
var index = 1;
if (vowels.Any(v => part.Substring(0, 1).ToLower() == v))
{
index = 0;
}
else if (new [] { "sh", "ch", "th", "tr", }.Contains(part.Substring(0, 2).ToLower()))
{
index = 2;
}
TranslateOutput.Text += " " + part.Substring(index) + part.Substring(0, index);
}
}
TranslateOutput.Text = TranslateOutput.Text.TrimEnd();
}

这将它归结为您实际需要的一个 foreach 循环。

您还可以让您的代码初始化 vowelsspecials 如下:

vowels.AddRange("aeiou".Select(x => x.ToString()));
specials.AddRange(@"`1234567890-=[]\;',./~!@#$%^&*()_+{}|:""<>?".Select(x => x.ToString()));

关于c# - Pig Latin Translator 吐出多行? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34377365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com