【发布时间】:2017-07-10 12:35:52
【问题描述】:
每次运行此代码时,我的计数变量总是从 48 开始。我将它们明确初始化为 0。我假设这与读取我的 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();
}
}
}
【问题讨论】:
-
int otherCounter = '0';那将如何编译? c#非常严格,如果确实有错误就不会编译,那就是错误 -
@Beginner 隐式转换从
char到int -
不管它是否有效......你为什么要这样做?只需使用数字零。
-
程序编译。我正在使用视觉工作室。