通过使用包含整数名称的String获取整数值

时间:2016-03-05 08:41:40

标签: 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 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();
    }

......但遗憾的是不成功。

我如何以这种方式访问​​整数并检查它的内容?

非常感谢。

2 个答案:

答案 0 :(得分: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 (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 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是最佳选择,或者如果您知道数据的确切数量,您也可以使用数组类型。

答案 1 :(得分:0)

基本上你应该为计数器使用某种集合并对其进行过滤。

这是使用数组的解决方案:

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!");
}