【问题标题】:How to force the user's input to lower case or any other way to make the user's input case insensitive [duplicate]如何强制用户输入小写或任何其他方式使用户输入不区分大小写[重复]
【发布时间】:2021-06-24 17:42:48
【问题描述】:

快速提问。 C# 的新手和实践做了一个简单的猜谜游戏,但试图让猜测不区分大小写。在下面的示例中,秘密词 =“cow”,但我希望“Cow”或“COW”也被接受。

我尝试使用 guess.ToLower(); 将guess 变量强制为小写,但它不起作用。有什么建议或替代方案吗?

谢谢大家。非常感谢,

罗里

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecretWord
{
    class Program
    {
        static void Main(string[] args)
        {

            string secretWord = ("cow");

            string guess = "", hint0 = "It's an animal!", hint1 = "Some of us have black and white coats!", hint2 = "You can often find me on a farm!", hint3 = "Moo!";
            
            guess.ToLower();
            
            int guesscount = 1;
            
            while (guess != secretWord && guesscount != 5)
            {
                
                if (guesscount == 1)
                {
                    Console.WriteLine($"Guess the secret word! {hint0}");
                    Console.Write("Enter a guess: ");
                    guess = Console.ReadLine();
                    guess.ToLower();
                    if (guess == secretWord)
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine($"3 more tries. Here is a hint: {hint1}.");
                    }
                }
                if (guesscount == 2)
                {
                    Console.Write("Enter a guess: ");
                    guess = Console.ReadLine();
                    if (guess == secretWord)
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine($"2 more tries. Here's another hint: {hint2}.");
                    }
                }
                if (guesscount == 3)
                {
                    Console.Write("Enter a guess: ");
                    guess = Console.ReadLine();
                    if (guess == secretWord)
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine($"One last try and one last hint: {hint3}.");
                    }

                }
                if (guesscount == 4)
                {
                    Console.Write("Enter a guess: ");
                    guess = Console.ReadLine();
                    if (guess == secretWord)
                    {
                        break;
                    }
                    else
                    {
                        Console.Write($"Too bad! ");
                    }

                }
                guesscount = guesscount + 1;

            };

            if (guess == secretWord) 
            { 
                Console.WriteLine("You win!");
            }
            else 
            {
                Console.Write("Better luck next time!");
            }

            Console.ReadKey();


        }
    }
}

【问题讨论】:

  • 您也可以投票赞成接受的答案:)
  • 我显然没有足够高的声誉......一旦我做到了。再次感谢。
  • 哦!没关系,非常感谢你:)

标签: c#


【解决方案1】:

简单:

if (guess.Equals(secretWord, StringComparison.OrdinalIgnoreCase))

【讨论】:

    【解决方案2】:

    C# 中的字符串是不可变的,这意味着它们无法更改。每当您想更改字符串时,您都在创建一个 NEW 字符串。

    注意ToLower() 方法返回一个字符串。这意味着它将接受您当前的字符串,并返回一个全部降低的新字符串。但默认情况下,您的原始变量没有被修改。

    为了更清楚地展示这一点,请尝试这样做:

    var guessToLower = guess.ToLower()
    

    你会看到guessToLower的guess都是小写的,但是guess变量没有被修改。

    因此,为了使您的代码基本保持不变,您需要将 ToLower 中的 new 字符串分配回您的guess 变量。它看起来像这样:

    guess = guess.ToLower();
    

    另一种选择是不使用ToLower(),而是使用 .Equals 方法并告诉它忽略大小写(正如 Alexandru 所展示的那样)。

    【讨论】:

    • 谢谢。这是一个非常清晰的解释,可以帮助我了解实际发生的情况。非常感谢您付出如此详细的解释!现在就试试看:)
    • 没问题!不要忘记将此标记为已回答,以免它保持打开状态。
    【解决方案3】:

    您可以使用以下条件:

     if(String.Equals(guess, secretWord, StringComparison.OrdinalIgnoreCase))
      {
    
      }
    
     Instead of it:
    
     if(guess == secretWord) 
      { 
             
      }
    

    【讨论】:

    • 非常感谢。这也很有效。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2022-12-06
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多