C# 获取一个整数';通过使用包含整数名称的字符串来设置值
我试过:C# 获取一个整数';通过使用包含整数名称的字符串来设置值,c#,string,variables,int,C#,String,Variables,Int,我试过: 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 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 ((FindName(sb.ToString()) as int).Equals(4)) // Problem line here
{
MessageBox.Show("You have four-of-a-kind!");
}
sb.Clear();
}
…但不幸的是没有成功
如何以这种方式访问整数并检查其内容
非常感谢。这是通过以下列出的几种方式完成的 第一种方法 第二种方法 我不明白你为什么用
if (sb.ToString().Contains("14"))
循环中的条件
从我的角度来看,你可以通过字典来做到这一点
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();
}
字典计数器=新字典();
计数器。添加(“计数器14”,0);
计数器。添加(“计数器13”,0);
计数器。添加(“计数器12”,0);
计数器。添加(“计数器11”,0);
计数器。添加(“计数器10”,0);
计数器。添加(“计数器9”,4);
计数器。添加(“计数器8”,0);
计数器。添加(“计数器7”,0);
计数器。添加(“计数器6”,0);
计数器。添加(“计数器5”,4);
计数器。添加(“计数器4”,0);
计数器。添加(“计数器3”,0);
计数器。添加(“计数器2”,0);
//StringBuilder sb=新的StringBuilder();
//对于(int i=14;i>=2;i--)
foreach(counter.Keys中的字符串str)
{
//附加,附加附加附加(即ToString());
//如果(sb.ToString().包含(“14”)//此处的问题行
如果(计数器[str]==4)
{
Show(“你有四个同类!”);
}
//(某人清楚地);
}
或者只是从列表中选择
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();
}
列表计数器=新列表();
计数器。添加(0);
计数器。添加(0);
计数器。添加(0);
计数器。添加(4);
计数器。添加(0);
计数器。添加(0);
计数器。添加(0);
计数器。添加(4);
计数器。添加(0);
//对于(int i=14;i>=2;i--)
foreach(计数器中的int值)
{
//附加,附加附加附加(即ToString());
//如果(sb.ToString().包含(“14”)//此处的问题行
如果(值==4)
{
Show(“你有四个同类!”);
}
//(某人清楚地);
}
使用字典是因为我不知道,若你们想要记录的计数器号?else列表是最好的选择,或者如果您知道确切的数据量,也可以使用数组类型。基本上,您应该为计数器使用某种类型的集合,并对其进行筛选 以下是使用阵列的解决方案:
var counters = new[] {0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0, 0, };
// Test for four-of-a-kind
foreach (var matchingCounter in counters.Where(counter => counter == 4))
{
MessageBox.Show("You have four-of-a-kind!");
}
为什么不使用数组呢?回答你的问题。不,这根本不可能,因为局部变量不会按名称进行编译。我甚至没有想到使用数组,谢谢@M.kazemakhgary我认为在JavaScript中它是有效的。在那里使用eval()函数。@Dhani Kris Frohling这回答了你的问题吗?如果是,请标记为答案:-)
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();
}
var counters = new[] {0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0, 0, };
// Test for four-of-a-kind
foreach (var matchingCounter in counters.Where(counter => counter == 4))
{
MessageBox.Show("You have four-of-a-kind!");
}