gpt4 book ai didi

c# - C# 石头、剪刀、布

转载 作者:行者123 更新时间:2023-12-02 19:06:46 26 4
gpt4 key购买 nike

我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如果我遇到任何其他问题,请告诉我。

using System;

namespace Rockpaperscissors
{
class Program
{
static void Main(string[] args)
{
string inputPlayer, Computer;
int randomnum;
string loop;
bool keepPlaying = true;

while (keepPlaying)
{

int wins = 0;
int Loses = 0;
int ties = 0;


while (keepPlaying)
{

Random myRandomObject = new Random();
randomnum = myRandomObject.Next(1, 4);
Console.Write("To play: enter R for Rock, S for Scissors, P for Paper.");
inputPlayer = Console.ReadLine();
inputPlayer = inputPlayer.ToUpper();

switch (randomnum)
{

case 1:
Computer = "Rock";
Console.WriteLine("The computer played Rock");
if (inputPlayer == "R")
{
Console.WriteLine("Tie!!\n\n");
ties++;
}
else if (inputPlayer == "P")
{
Console.WriteLine("You win!!\n\n");
wins++;
}
else if (inputPlayer == "S")
{
Console.WriteLine("Computer wins!!\n\n");
Loses++;
}
break;
case 2:
Computer = "Paper";
Console.WriteLine("The computer played Paper");
if (inputPlayer == "P")
{
Console.WriteLine("Tie!!\n\n");
ties++;
}
else if (inputPlayer == "R")
{
Console.WriteLine("Computer wins!!\n\n");
Loses++;
}
else if (inputPlayer == "S")
{
Console.WriteLine("You win!!\n\n");
wins++;
}
break;
case 3:
Computer = "Scissors";
Console.WriteLine("The computer played Scissors");
if (inputPlayer == "S")
{
Console.WriteLine("Tie!!\n\n");
ties++;
}
else if (inputPlayer == "R")
{
Console.WriteLine("You win!!\n\n");
wins++;
}
else if (inputPlayer == "P")
{
Console.WriteLine("Computer wins!!\n\n");
Loses++;
}
break;
default:
Console.WriteLine("Please enter a correct entry");
break;
}

Console.WriteLine("Scores:\tWins:\t{0},\tLoses:\t{1},\tties:\t{2}", wins, Loses, ties);

Console.WriteLine("Would you like to continue playing? (y/n)");
loop = Console.ReadLine();
if (loop == "y")
{
keepPlaying = true;

}
else if (loop == "n")
{
keepPlaying = false;
}
else
{

}

}

}

}
}
}

请帮忙!

最佳答案

这是我在控制台应用程序中使用过的一种非常有帮助的方法。实际上,我有几个用于从用户获取类型(例如 intdouble)。它接受一个提示,该提示显示给用户,并包含一个可选的验证器方法,它将针对输入运行以查看其是否有效。

这是字符串输入的一个:

public static string GetStringFromUser(string prompt, Func<string, bool> validator = null)
{
var isValid = true;
string result;

do
{
if (!isValid)
{
WriteLineColor("Invalid input, please try again.", ConsoleColor.Red);
}
else isValid = false;

Console.Write(prompt);
result = Console.ReadLine();
} while (validator != null && !validator.Invoke(result));

return result;
}

就您而言,您只需这样调用它:

string playerInput = GetStringFromUser(
"To play: enter R for Rock, S for Scissors, P for Paper: ",
x => x.ToUpper() == "R" || x.ToUpper() == "S" || x.ToUpper() == "P");

Console.WriteLine($"You entered: {playerInput}");

Console.Write("\nPress any key to continue...");
Console.ReadKey();

这是示例输出:

enter image description here

关于c# - C# 石头、剪刀、布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64938163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com