如何修复在控制台中始终显示 0 的输出?

huangapple go评论115阅读模式
英文:

How do I fix the output from always displaying 0 in the console?

问题

我正在编写一个程序,它应该将一个字符串分成相同字母的组,然后对每个组,它应该计算大写字母和小写字母的数量,然后将它们相乘,然后将每个组的值相加到整个字符串的总值中并输出该总值。

例如,如果字符串是"ccCeEEEcBbBbb",在第一组字母(ccC)中有两个小写字母和一个大写字母(2 x 1),在第二组字母(eEEE)中有一个小写字母和三个大写字母(1 x 3),在第三组字母(c)中有一个小写字母和没有大写字母(1 x 0),在最后一组字母(BbBbb)中有三个小写字母和两个大写字母(3 x 2) - 因此总和将是(2 x 1)+(1 x 3)+(1 x 0)+(3 x 2)= 2 + 3 + 0 + 6 = 11。

以下是迄今为止我编写的代码:

//string[] letters = { "a" };
//string[] letters =  { "aA" };
//string[] letters = { "Aa" };
//string[] letters = { "AAa" };
//string[] letters =  { "aaA" };
//string[] letters = { "aAa" };
//string[] letters =  { "AaA" };
//string[] letters = { "aAabB" };
//string[] letters =  { "aAabBB" };
//string[] letters = { "AAaBbBaaA" };
string[] letters =  { "AaAbAaA" };


//string[] letters = { "ccCeEEEcBbBbb" };

foreach (string letter in letters)
{
    int total = 0;
    int upper = 0;
    int lower = 0;
    StringBuilder sb = new StringBuilder(letter.Length);
    char lastChar = '\0';
    foreach (char c in letter)
    {
        if (c != lastChar)
        {
            sb.Append(c);
            lastChar = c;
        }
    }
    string data = sb.ToString().ToLower();
    for (int i = 0; i < data.Length; i++)
    {
        if (char.IsUpper(data[i]))
        {
            upper++;
        }
        else
        {
            lower++;
        }
    }
    total = upper * lower;
    Console.WriteLine(total);
}

到目前为止我遇到的问题

控制台中的输出始终显示为0,无论测试数据如何。

Argument                             : Expected           : Actual
----------------------------------------------------------------------
a                                    : 0                  : 0 
aA                                   : 1                  : 0
Aa                                   : 1                  : 0
AAa                                  : 2                  : 0
aaA                                  : 2                  : 0

我在调试器中的监视窗口中查看了所有变量的值,但仍然不知道错误可能在哪里?

我有一种预感,char 的使用可能是问题所在。

英文:

I am writing a program that should split a string into groups of the same letter, then for each group it should count the number of uppercase and lowercase letters and multiply them together, then add the value for each group to a total for the entire string and output that total value.

So for example if the string was "ccCeEEEcBbBbb" in the first group of letters (ccC) there are two lowercase letters and one uppercase letter (2 x 1), in the second group of letters (eEEE) there is one lowercase letter and three uppercase letters (1 x 3), in the third group of letters (c) there is one lowercase letter and no uppercase letters (1 x 0) and in the last group of letters (BbBbb) there are three lowercase letters and two uppercase letters (3 x 2) - so the total would be (2 x 1) + (1 x 3) + (1 x 0) + (3 x 2) = 2 + 3 + 0 + 6= 11.

Here is the code I've written so far:

//string[] letters = { "a" };
//string[] letters =  { "aA" };
//string[] letters = { "Aa" };
//string[] letters = { "AAa" };
//string[] letters =  { "aaA" };
//string[] letters = { "aAa" };
//string[] letters =  { "AaA" };
//string[] letters = { "aAabB" };
//string[] letters =  { "aAabBB" };
//string[] letters = { "AAaBbBaaA" };
string[] letters =  { "AaAbAaA" };


//string[] letters = { "ccCeEEEcBbBbb" };

foreach (string letter in letters)
{
    int total = 0;
    int upper = 0;
    int lower = 0;
    StringBuilder sb = new StringBuilder(letter.Length);
    char lastChar = '
//string[] letters = { "a" };
//string[] letters =  { "aA" };
//string[] letters = { "Aa" };
//string[] letters = { "AAa" };
//string[] letters =  { "aaA" };
//string[] letters = { "aAa" };
//string[] letters =  { "AaA" };
//string[] letters = { "aAabB" };
//string[] letters =  { "aAabBB" };
//string[] letters = { "AAaBbBaaA" };
string[] letters =  { "AaAbAaA" };
//string[] letters = { "ccCeEEEcBbBbb" };
foreach (string letter in letters)
{
int total = 0;
int upper = 0;
int lower = 0;
StringBuilder sb = new StringBuilder(letter.Length);
char lastChar = '\0';
foreach (char c in letter)
{
if (c != lastChar)
{
sb.Append(c);
lastChar = c;
}
}
string data = sb.ToString().ToLower();
for (int i = 0; i < data.Length; i++)
{
if (char.IsUpper(data[i]))
{
upper++;
}
else
{
lower++;
}
}
total = upper * lower;
Console.WriteLine(total);
}
'; foreach (char c in letter) { if (c != lastChar) { sb.Append(c); lastChar = c; } } string data = sb.ToString().ToLower(); for (int i = 0; i < data.Length; i++) { if (char.IsUpper(data[i])) { upper++; } else { lower++; } } total = upper * lower; Console.WriteLine(total); }

The problem I am facing so far

The output in the console always shows 0 regardless of the test data.

Argument                             : Expected           : Actual
----------------------------------------------------------------------
a                                    : 0                  : 0 
aA                                   : 1                  : 0
Aa                                   : 1                  : 0
AAa                                  : 2                  : 0
aaA                                  : 2                  : 0

I went to the watch window in the debugger and all the variables comes up with values but I still don't know where the error might be?

I have a hunch that the use of char might be the problem.

答案1

得分: 1

你可以实现一个简单的有限状态机:

private static int SolveIt(string value) {
int result = 0;

char prior = '\0';

int lower = 0;
int upper = 0;

foreach (char letter in value) {
if (prior != char.ToLower(letter)) {
result += lower * upper;

  lower = 0;
  upper = 0;

  prior = char.ToLower(letter);
}

if (char.IsLower(letter))
  lower += 1;
else
  upper += 1;

}

return result + lower * upper;
}


演示:

int result = SolveIt("ccCeEEEcBbBbb");

Console.WriteLine(result);


输出:

11

英文:

You can implement a simple Finite State Machine:

private static int SolveIt(string value) {
  int result = 0;

  char prior = '
private static int SolveIt(string value) {
int result = 0;
char prior = '\0';
int lower = 0;
int upper = 0;
foreach (char letter in value) {
if (prior != char.ToLower(letter)) {
result += lower * upper;
lower = 0;
upper = 0;
prior = char.ToLower(letter);
}
if (char.IsLower(letter))
lower += 1;
else
upper += 1;
}
return result + lower * upper;
}
'; int lower = 0; int upper = 0; foreach (char letter in value) { if (prior != char.ToLower(letter)) { result += lower * upper; lower = 0; upper = 0; prior = char.ToLower(letter); } if (char.IsLower(letter)) lower += 1; else upper += 1; } return result + lower * upper; }

Demo:

int result = SolveIt("ccCeEEEcBbBbb");

Console.WriteLine(result);

Output:

11

huangapple
  • 本文由 发表于 2023年8月10日 23:14:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定