为什么我的计数器变量不显示为零? C#

时间:2017-02-21 03:16:04

标签: c# loops char

每次运行此代码时,我的计数变量总是从48开始。我将它们清楚地初始化为0.我假设这与读取我的char变量有关吗?我是否坚持将输入作为字符串读取并将其转换为char?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace CountLowers
{
    class Program
    {
        static void Main(string[] args)
        {
            char choice;
            int otherCounter = '0';
            int lowerCounter = '0';

            do
            {
                WriteLine("Enter an Upper or Lower Case Charactor");
                Write("or Enter the '}' key to stop and view results > ");
                choice = Console.ReadKey().KeyChar;

                if (Char.IsLower(choice))
                {
                    WriteLine("\n\n\t" + choice + " is a Lower Case Character\n");
                    lowerCounter = lowerCounter + 1;
                }
                else if (choice != '}')
                {
                    WriteLine("\n\n\tYou did not enter a Lower Case Character\n");
                    otherCounter = otherCounter + 1;
                }            
                else
                {
                    WriteLine("\n\n\tRESULTS\n");
                    WriteLine("You typed in " + lowerCounter + " Lower Case Charactors");
                    WriteLine("\nYou typed in " + otherCounter + " Other Charactors");
                }
            } while (choice != '}');

            Console.ReadKey();
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您使用零字符'0'而不是整数零。删除单引号。

    int otherCounter = 0;
    int lowerCounter = 0;