-4
static void Main()
{
    int x = Program.char_value("cat");
    Console.WriteLine(x);
    Console.ReadLine();
}

public static int char_value(string user_input)
{
    char a = 'a';
    char c = 'c';
    char t = 't';
    int user_input_length = user_input.Length;
    int char_value = 0;
    if (user_input == "" && user_input_length <= 5)
    {
        foreach (char z in user_input)
        {
            if (z == a)
            {
                char_value = 1;
            }
            if (z == c)
            {
                char_value += 3;
            }
            if (z == t)
            {
                char_value += 20;
            }
        }
        return char_value;
    }
    return char_value;
}
4

1 回答 1

2

你为什么不简单地将你的转换char成一个int呢?

int sum = 0;
foreach (char z in user_input.ToLower())
    sum += Convert.ToInt32(z) - 96; // ensures that 'a' has value 1, 'b' has value 2, etc.

return sum;
于 2013-02-19T15:13:03.507 回答