【发布时间】: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#