Assign integer value to letter

后端 未结 3 1847
别那么骄傲
别那么骄傲 2021-01-14 05:43

So i have this problem. I have Text Box with text containing 12 numbers. So for example 012345678912. Now i don\'t know how to assign the first, then the second .... and so

相关标签:
3条回答
  • 2021-01-14 06:36

    there are plenty of approaches that you can adapt to achieve this. But if you really want to declare the variables as a,b,c,d,etc.

    you can simply declare all these letters first. Once you have done that you can use for each loop as follows

    Code

                int a, b, c, d, e, f, g, h, i, j, k, l;
    
    // conversion of whole value in text box to single integers
                char[] digits_array = TextBox1.Text.ToCharArray(); 
    
    //Now just declare each variable as you want there are several ways to do it    
                a = int.Parse(digits_array[0].ToString());
                b = int.Parse(digits_array[1].ToString());
                c = int.Parse(digits_array[2].ToString());
                d = int.Parse(digits_array[3].ToString());
                e = int.Parse(digits_array[4].ToString());
                f = int.Parse(digits_array[5].ToString());
                g = int.Parse(digits_array[6].ToString());
                h = int.Parse(digits_array[7].ToString());
                i = int.Parse(digits_array[8].ToString());
                j = int.Parse(digits_array[9].ToString());
                k = int.Parse(digits_array[10].ToString());
                l = int.Parse(digits_array[11].ToString());
    

    Now You can simply use these values in your Formaula the main function that i used here is .ToCharArray() Function . I have checked the script and its working fine Hoever the script seems pretty Long So I want other developers to help me out to squeeze this code. I tried Plenty of things apart from this method but none of them worked.

    0 讨论(0)
  • 2021-01-14 06:45
    int x = int.Parse(txtNumbers.Text);
    
    int a[12];
    for(int i=0;i<12;i++)
    {
        a[i] = x %12;
        x = x/10;
    }
    
    0 讨论(0)
  • 2021-01-14 06:49

    This question seems a bit broad. There's quite a lot of ways to do what you're suggesting.

    One way would simply be to assign a - l to values, as variables, such as:

    int a = 1;
    int b = 2;
    int c = 3;
    ...
    

    Another way would be with an Dictionary

    Dictionary<char, int> Alpha = new Dictionary<char,int>() 
    { 
       {'a', 1}, {'b', 2}, {'c', 3} ...
    };
    

    You could use an enumeration

    public enum Alphabet
    {
      A = 1,
      B = 2,
      C = 3,
      ...
    }
    
    Alphabet alpha = Alphabet.B;
    Console.WriteLine((int)alpha);
    

    Etc.

    0 讨论(0)
提交回复
热议问题