首页 > 解决方案 > 使用 String.ToUpper() 时程序退出;在包含空格的字符串上

问题描述

让我开始说我是 C# 的新手。

我目前正在制作我的第一个命令行应用程序,它在当前状态下可以做两件事。其中一个是计算器,我需要更多的学习才能真正使它工作,另一个是字符串大写字母。

我有一个string nameCapInput = Console.Readline()接受用户输入的方法,然后对其进行分析以确保不允许使用数字:

using System;
using System.Linq;

namespace First_Console_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("My first ever console application - 2020/2/26\n\n\n");
        programSel:
            Console.WriteLine("What do you want to do?\n");
            Console.WriteLine("1. Calculate Numbers \n2. Capitalize Letters/Strings");
            Console.WriteLine("Input your desired action:");
            var inputVar = Console.ReadLine();
            switch (inputVar)
            {
                case "1":
                    //Calculator code goes here
                    Console.WriteLine("Number 1 succeeded, opening calculator... Stand by");
                    Console.WriteLine("Calulator Loaded.");
                    Console.WriteLine("Doesn't work right now. Type \"exit\" to get back to the \"what do you want to do\" page.");
                    //Code goes here when I have learned the proper methods
                calcInput:
                    var calcInput = Console.ReadLine();
                    if (calcInput == "exit")
                    {
                        goto programSel;
                    } else
                    {
                        Console.WriteLine("Unknown command. Type \"exit\" to get back to the \"what do you want to do\" page.");
                        goto calcInput;
                    }
                case "2":
                    Console.WriteLine("Loading string capitalizer...");
                    Console.WriteLine("Type any string made of letters only without spaces, because if you use spaces, the program will exit. The output will make them all uppercase. Type \"exit\" to get back to the \"what do you want to do\" page.");
                inputCap:
                    string nameCapInput = Console.ReadLine();
                    bool containsInt = nameCapInput.Any(char.IsDigit);
                    bool isMadeOfLettersOnly = nameCapInput.All(char.IsLetter);
                    if (nameCapInput == "exit")
                    {
                        goto programSel;
                    }
                    else if (containsInt)
                    {
                        Console.WriteLine("You can't capitalize numbers. Use letters only. Try again.");
                        goto inputCap;
                    }
                    else if (isMadeOfLettersOnly)
                    {
                        string upper = nameCapInput.ToUpper();
                        Console.WriteLine($"The uppercase version of your entered text is: {upper}");
                        goto inputCap;
                    }
                    break;
                    }
            }
        }
}

现在,一切正常,它将我放入其中的所有内容都大写,除了带有空格的字符串。当我输入一个带有空格的字符串时,程序会以代码 0 退出。我还不太擅长 C#,所以我真的不知道从这里往哪里走。任何帮助表示赞赏。

每次我在 C# 中学习新东西时,我都会尝试将其实现到我的项目中,这样我就可以真正学习如何实现它,以了解何时以及如何使用我学到的东西。这是一个例子。

编辑:添加了其余的代码。非常感谢大家。我在这里学到了两件事:

  1. goto是个坏习惯
  2. 绝对需要开始学习调试我自己的代码。

标签: c#command-line.net-core-3.1

解决方案


检查文档:https ://docs.microsoft.com/en-us/dotnet/api/system.char.isletter?view=netframework-4.8

根据该IsLetter函数的文档,该空间不包含在 return true 案例中。

我建议您为此使用正则表达式或将您的最后一个案例更改为

else if (!containsInt)
{
    var upper = nameCapInput.ToUpper();
    Console.WriteLine($"The uppercase version of your entered text is: {upper}");
    goto inputCap;
}

还要检查 goto 的文档:https ://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/goto

goto 语句将程序控制直接传递给带标签的语句。

goto 的一个常见用途是将控制转移到特定的 switch-case 标签或 switch 语句中的默认标签。

goto 语句对于跳出深度嵌套的循环也很有用。

你不是在任何这种情况下,所以你不应该使用它。


推荐阅读