2017-02-21 2 views
0

私はこのコードを実行するたびに、私のカウント変数は常に48で始まります。私はそれらを明確に0で初期化しています。これは私のchar変数の読み込みと関係があると仮定していますか?私は文字列として入力を読み込み、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';'どのようにコンパイルするのですか? エラーが発生してエラーが発生した場合、コンパイルされないように、非常に厳しいです。 – Beginner

+2

@Beginner暗黙的に 'char'から' int'へキャスト – Vache

+0

それはどういうことでしょう?数字のゼロを使用してください。 –

答えて

4

整数ゼロの代わりにゼロ文字'0'を使用しています。一重引用符を削除します。

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

ありがとうございます。私はそれが私が行方不明だったことが分かった。私は肯定的な解決に感謝します。なぜ他のポスターが役に立たなかったのか分かりません。 (あまり失礼) – codeRed

関連する問題