这是通过下面列出的几种方式完成的
第一种方法
int counter14 = 0;
int counter13 = 0;
int counter12 = 0;
int counter11 = 0;
int counter10 = 0;
int counter9 = 4;
int counter8 = 0;
int counter7 = 0;
int counter6 = 0;
int counter5 = 4;
int counter4 = 0;
int counter3 = 0;
int counter2 = 0;
// Test for four-of-a-kind
StringBuilder sb = new StringBuilder();
for (int i = 14; i >= 2; i--)
{
sb.Append("counter").Append(i.ToString());
if (sb.ToString().Contains('4')) // Problem line here
{
MessageBox.Show("You have four-of-a-kind!");
}
//sb.Clear();
}
第二种方法
int counter14 = 0;
int counter13 = 0;
int counter12 = 0;
int counter11 = 0;
int counter10 = 0;
int counter9 = 4;
int counter8 = 0;
int counter7 = 0;
int counter6 = 0;
int counter5 = 4;
int counter4 = 0;
int counter3 = 0;
int counter2 = 0;
// Test for four-of-a-kind
StringBuilder sb = new StringBuilder();
for (int i = 14; i >= 2; i--)
{
sb.Append("counter").Append(i.ToString());
if (sb.ToString().Contains("14")) // Problem line here
{
MessageBox.Show("You have four-of-a-kind!");
}
//sb.Clear();
}
我不明白你为什么使用
if (sb.ToString().Contains("14"))
for 循环中的条件?
从我的角度来看,你可以通过字典来做到这一点
Dictionary<string, int> counter = new Dictionary<string, int>();
counter.Add("counter14", 0);
counter.Add("counter13", 0);
counter.Add("counter12", 0);
counter.Add("counter11", 0);
counter.Add("counter10", 0);
counter.Add("counter9", 4);
counter.Add("counter8", 0);
counter.Add("counter7", 0);
counter.Add("counter6", 0);
counter.Add("counter5", 4);
counter.Add("counter4", 0);
counter.Add("counter3", 0);
counter.Add("counter2", 0);
//StringBuilder sb = new StringBuilder();
//for (int i = 14; i >= 2; i--)
foreach(string str in counter.Keys)
{
//sb.Append("counter").Append(i.ToString());
//if (sb.ToString().Contains("14")) // Problem line here
if(counter[str]==4)
{
MessageBox.Show("You have four-of-a-kind!");
}
//sb.Clear();
}
或者只是从列表中
List<int> counter = new List<int>();
counter.Add(0);
counter.Add(0);
counter.Add(0);
counter.Add(4);
counter.Add(0);
counter.Add(0);
counter.Add(0);
counter.Add(4);
counter.Add(0);
//for (int i = 14; i >= 2; i--)
foreach(int value in counter)
{
//sb.Append("counter").Append(i.ToString());
//if (sb.ToString().Contains("14")) // Problem line here
if(value==4)
{
MessageBox.Show("You have four-of-a-kind!");
}
//sb.Clear();
}
使用字典是因为我不知道,如果您想要记录的计数器编号? else List 是最好的选择,或者如果您知道确切的数据量,您也可以使用数组类型。