【问题标题】:Get an Integer's value by using a String containing the Integers name使用包含整数名称的字符串获取整数值
【发布时间】:2016-06-19 03:06:22
【问题描述】:

我试过了:

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

...可惜没有成功。

我怎样才能以这种方式访问​​ Integer 并检查它的内容?

非常感谢。

【问题讨论】:

  • 为什么不使用数组? dotnetperls.com/int-array
  • 并回答您的问题。不,这根本不可能,因为局部变量不会按名称编译。
  • 奥基,我明白了。我什至没有想到使用数组,谢谢@M.kazemAkhgary
  • 我认为在 JavaScript 中它可以工作。在那里使用 eval() 函数。

标签: c# string variables int


【解决方案1】:

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

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

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

【讨论】:

  • @Dhani Kris Frohling 这回答了你的问题吗?如果是这样,请标记为答案:-)
【解决方案2】:

这是通过下面列出的几种方式完成的

第一种方法

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 是最好的选择,或者如果您知道确切的数据量,您也可以使用数组类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 2019-11-23
    • 2020-07-02
    • 1970-01-01
    相关资源
    最近更新 更多