猪拉丁翻译吐出多行?C#
所以我有一个支持多个单词的Pig拉丁翻译。但每当我输入一些单词时(例如,我们将使用“香蕉苹果剪刀巧克力西奥多火车”),它会正确地吐出翻译的单词,但会重复!这是我的密码:猪拉丁翻译吐出多行?C#,c#,winforms,translators,C#,Winforms,Translators,所以我有一个支持多个单词的Pig拉丁翻译。但每当我输入一些单词时(例如,我们将使用“香蕉苹果剪刀巧克力西奥多火车”),它会正确地吐出翻译的单词,但会重复!这是我的密码: namespace Pig_Latin_Translator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } List&l
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);
}
}
namespace Pig\u拉丁翻译程序
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
列表元音=新列表();
列表特殊项=新列表();
私有void TranslateButton_单击(对象发送方,事件参数e)
{
String[]parts=TranslateBox.Text.Split();
foreach(特价中的字符串s)
{
if(TranslateBox.Text.Contains)| | TranslateBox.Text==“\”)
{
TranslateOutput.Text=“”;
显示(“无特殊字符!”、“警告!”、MessageBoxButtons.OK、MessageBoxIcon.Warning);
打破
}
其他的
{
foreach(部分中的字符串部分)
{
foreach(元音中的字符串v)
{
if(部分子串(0,1)==v)
{
TranslateOutput.Text=TranslateOutput.Text+“”+part+“ay”;
打破
}
其他的
{
如果(部分子串(0,2)=“sh”|部分子串(0,2)=“ch”|部分子串(0,2)=“th”|部分子串(0,2)=“tr”)
{
字符串开关P=part.Substring(2)+part.Substring(0,2);
TranslateOutput.Text=TranslateOutput.Text+“”+SwitchP+“ay”;
打破
}
其他的
{
字符串开关=部分子字符串(1)+部分子字符串(0,1);
TranslateOutput.Text=TranslateOutput.Text+“”+开关+“ay”;
打破
}
}
}
}
}
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
元音。加上(“a”);
元音。加上(“e”);
元音。加上(“i”);
元音。加上(“o”);
元音。加上(“u”);
特价商品。添加(“`”);
特殊条款。添加(“1”);
特价。添加(“2”);
特价。添加(“3”);
特价。添加(“4”);
特价。添加(“5”);
特殊条款。添加(“6”);
特价。添加(“7”);
特价。添加(“8”);
特价。添加(“9”);
特价。添加(“0”);
特价商品。添加(“-”);
添加(“=”);
特价。添加(“[”);
特价。添加(“]”);
特价。添加(@“\”);
特价。添加(“;”);
特价商品。添加(“”);
添加(“,”);
特价商品。添加(“.”);
特价。添加(“/”);
特价。添加(“~”);
特色菜。添加(“!”);
特价商品。添加(“@”);
特色菜。添加(“#”)号;
特价。添加(“$”);
特价。添加(“%”);
特价商品。添加(“^”);
特价商品。添加(“&”);
特价。添加(“*”);
特价商品。添加(“”);
特价商品。添加(“)”;
特价商品。添加(“”);
添加(“+”);
添加(“{”);
特价商品。添加(“}”);
特价。添加(“|”);
添加(“:”);
特色菜。添加(“\”);
特价商品。添加(“”);
特价。添加(“?”);
}
私有void关于按钮单击(对象发送者,事件参数e)
{
MessageBox.Show(“Pig拉丁语是一种假语言。它的工作原理是取第一个字母(如果是像‘th’或‘ch’这样的一对字母,则取两个)并将其结尾,除非第一个字母是元音。然后在结尾加上‘ay’。因此,‘bus’变成‘usbay’、‘thank’变成‘ankthay’、‘apple’变成‘appleay’。”,“关于:,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
如果输入“香蕉苹果剪刀巧克力西奥多火车”,则输出:
“ananabay appleay Earshay ocolatechay heodoreTay aintray”重复10次以上
顺便说一句:对不起,如果你不能回答,因为我知道有很多代码。但这并不重要,因为它仍然有用。只是这不应该发生,让我很紧张。我知道还有很多小故障,还有很多事情要做,但我想先解决这个问题。对于每个特殊字符,您都要对单词进行一次迭代。查看单词和翻译的
foreach
位于foreach
的内部,以检查文本框是否包含任何特殊字符
换句话说,你要为每个特殊角色翻译一次
您需要将
foreach(部分中的字符串部分)
移出foreach(特殊中的字符串s)
您的循环中有一点逻辑问题
你的外环:
foreach( string s in specials ) {
…正在循环遍历特殊字符列表中的所有42个字符
你的内环
foreach( String part in parts ) {
…然后执行42次。所以对于你的六个单词的例子,你实际上做了252次猪拉丁语转换
如果从外部提取内部循环,结果会更好。像这样:
foreach( string s in specials ) {
if( TranslateBox.Text.Contains( s ) || TranslateBox.Text == "\"" ) {
TranslateOutput.Text = "";
MessageBox.Show( "No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning );
return;
}
}
String[] parts = TranslateBox.Text.Split();
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 TranslateButton_Click( object sender, EventArgs e )
{
char[] specials = "`1234567890-=[]\";',./~!@#$%^&*()_+{}|:\\<>?".ToArray();
char[] vowels = "aeiou".ToArray();
TranslateOutput.Text = String.Empty;
if( TranslateBox.Text.IndexOfAny( specials ) > -1 ) {
MessageBox.Show( "No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning );
return;
}
String[] parts = TranslateBox.Text.Split();
foreach( String part in parts ) {
int firstVowel = part.IndexOfAny( vowels );
if( firstVowel > 0 ) {
TranslateOutput.Text += part.Substring( firstVowel ) + part.Substring( 0, firstVowel ) + "ay ";
}
else {
TranslateOutput.Text += part + "ay ";
}
}
TranslateOutput.Text = TranslateOutput.Text.TrimEnd();
}
private void TranslateButton_单击(对象发送方,事件参数e)
{
char[]specials=“`1234567890-=[]\”;,./~!@$%^&*():\ \?”.ToArray();
char[]元音=“aeiou.ToArray();
TranslateOutput.Text=String.Empty;
if(TranslateBox.Text.IndexOfAny(特价)>-1){
显示(“无特殊字符!”、“警告!”、MessageBoxButtons.OK、MessageBoxIcon.Warning);
返回;
}
String[]parts=TranslateBox.Text.Split();
foreach(部分中的字符串部分){
我
foreach (string s in specials)
foreach (String v in vowels)
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();
}
vowels.AddRange("aeiou".Select(x => x.ToString()));
specials.AddRange(@"`1234567890-=[]\;',./~!@#$%^&*()_+{}|:""<>?".Select(x => x.ToString()));