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();
}
}
}
'int otherCounter ='0';'那会怎么编译? c#非常严格,它不会编译,如果它确实有错误,并且是错误 – Beginner
@Beginner从'char'到'int'的隐式转换 – Vache
无论它是否有效......您为什么要这样做?只需使用数字零。 –