簡體   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