繁体   English   中英

使用 String.ToUpper() 时程序退出; 在一个有空格的字符串上

[英]Program exiting when using String.ToUpper(); on a string that has spaces in it

首先让我说我是 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. 绝对需要开始学习调试我自己的代码。

检查文档: 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 语句对于摆脱深度嵌套的循环也很有用。

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

问题的关键是您检查输入是否包含字母(而不是空格)。 一个简单的解决方法是稍微改变你的 LINQ。

bool isMadeOfLettersOnly = nameCapInput.All(c => char.IsLetter(c) || char.IsWhiteSpace(c));

所以现在输入的字母空格将被认为是有效的。

此外,您使用goto是一个非常糟糕的主意。 一般来说,永远不应该有任何理由使用goto

要解决此问题,请使用 while 循环和方法:

public static void Main()
{
    bool exit = false;
    do {
        exit = ProcessInput();
    }
    while(!exit);
}

private static bool ProcessInput()
{
    string nameCapInput = Console.ReadLine();

    bool containsInt = nameCapInput.Any(char.IsDigit);
    bool isMadeOfLettersOnly = nameCapInput.All(c => char.IsLetter(c) || char.IsWhiteSpace(c));

    if (nameCapInput.Equals("exit", StringComparison.CurrentCultureIgnoreCase))
    {
        return true; //exiting so return true
    }
    else if (containsInt)
    {
        Console.WriteLine("You can't capitalize numbers. Use letters only. Try again.");
    }
    else if (isMadeOfLettersOnly)
    {
        string upper = nameCapInput.ToUpper();
        Console.WriteLine("The uppercase version of your entered text is: {0}", upper);
    }   
    return false; //no exit, so return false
}

这只是一个快速重构,你可以让它变得更好。

在这里摆弄

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM