访问字符串的索引,但没有给出正确的答案

时间:2018-12-22 15:48:54

标签: c#

通过int digit = number [j]访问数字的索引,但不是数字,而是输入两位数字51、53、54等。我的输入是number = 3675356291,pairlength = 5,noofdigits = 10

public static long Highest_multiple(string number, int pairlength, int noofdigits)
            {
                int noofpairs = (noofdigits - pairlength) + 1;
                long[] multiplied_result = new long[noofpairs];
                int p = 0;
                number.ToCharArray();
                for( int i = 0; i < noofpairs; i ++)                      // i < noofpairs so that i don't get indexoutofrange exception, as I know how many pairs will be there.
                {
                    multiplied_result[i] = 1;
                    for( int j = i; j < (pairlength + i); j ++)
                    {
                        int digit = number[j];
                        Console.WriteLine(digit);
                        multiplied_result[i] = multiplied_result[i] * digit;
                    }
                }
                long answer = multiplied_result.Max();
                return answer; 
            }

1 个答案:

答案 0 :(得分:0)

看看ASCII table

char转换为int时,将得到其ASCII十进制值,而不是 not 实际键入的字符。 (尝试使用完全由字母组成的字符串,您仍然可以从中获得数字。)

此:

int digit = number[j];

应改为something like this

double digit = Char.GetNumericValue(number[j]);

请注意,这将为非数字字符返回-1