2017-02-21 76 views
0

每次我运行这段代码时,我的计数变量总是从48开始。我将它们清楚地初始化为0.我假设这与读取char变量有关吗?我坚持阅读输入作为一个字符串,并将其转换为字符?为什么我的计数器变量不显示零? c#

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

'int otherCounter ='0';'那会怎么编译? c#非常严格,它不会编译,如果它确实有错误,并且是错误 – Beginner

+2

@Beginner从'char'到'int'的隐式转换 – Vache

+0

无论它是否有效......您为什么要这样做?只需使用数字零。 –

回答

4

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

int otherCounter = 0; 
    int lowerCounter = 0; 
+1

谢谢。我知道这很简单,我错过了。我赞赏积极的解决方案。不确定为什么其他海报不会有帮助。 (不太粗鲁) – codeRed