第二个用户输入的验证不能正常工作,并且我被告知有很多不必要的代码
本文关键字:被告 代码 不必要 不能 验证 输入 用户 常工作 工作 第二个 | 更新日期: 2023-09-27 18:12:24
这是我第一年学习计算机编程课程,我当前的任务之一是创建一个可行的石头、剪子、布游戏。
我有它的大部分工作,但我的老师说,我有很多不必要的代码,实际上没有告诉我它是什么。我想把这个流程做到最好,因为我不满足于现有的工作。
同样,用户输入的第二次验证(用户做出选择,程序运行并显示谁获胜,然后被要求再次做出选择)不像第一次那样工作,我不知道出了什么问题。我一直在看这个在过去的两个星期,不能弄清楚,所以任何帮助是感激的。
我的代码如下:
namespace Assignment04
{
class Program
{
static void Main(string[] args)
{
// Declare variables
int gamesPlayed = 0, userWins = 0, computerWins = 0, draws = 0, userSelection, computerSelection;
bool inputIsValid = false;
// Prompt user
Console.WriteLine("Welcome to the Rock, Paper, Scissors game!");
Console.WriteLine("1 - Rock");
Console.WriteLine("2 - Paper");
Console.WriteLine("3 - Scissors");
Console.WriteLine("4 - Quit program and view record");
// Create a loop to validate user's selection
do
{
Console.Write("Please make a selection: ");
// loop and test using TryParse()
while (!int.TryParse(Console.ReadLine(), out userSelection))
{
// invalid data type
Console.WriteLine("Invalid input.");
Console.Write("Please make a selection: ");
}
// test if input is within acceptable range
if (userSelection >= 1 && userSelection <= 4)
{
inputIsValid = true;
}
else
{
// valid integer, but out of acceptable range
Console.WriteLine("Number out of range.");
}
} while (!inputIsValid);
// Display user's choice
while (userSelection >= 1 && userSelection <= 3)
{
if (userSelection == 1)
{
Console.WriteLine("'nYou have selected Rock");
gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 2)
{
Console.WriteLine("'nYou have selected Paper");
gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 3)
{
Console.WriteLine("'nYou have selected Scissors");
gamesPlayed = gamesPlayed + 1;
}
// Generate computer's choice
Random randomNumber = new Random();
computerSelection = randomNumber.Next(1, 4);
// Display computer's choice
if (computerSelection == 1)
{
Console.WriteLine("Computer has chosen Rock");
}
else if (computerSelection == 2)
{
Console.WriteLine("Computer has chosen Paper");
}
else if (computerSelection == 3)
{
Console.WriteLine("Computer has chosen Scissors");
}
// Calculate and display who wins
if (userSelection == computerSelection)
{
draws = draws + 1;
}
else if (userSelection == 1 && computerSelection == 3 || userSelection == 2 && computerSelection == 1 || userSelection == 3 && computerSelection == 2)
{
userWins = userWins + 1;
}
else if (userSelection == 1 && computerSelection == 2 || userSelection == 2 && computerSelection == 3 || userSelection == 3 && computerSelection == 1)
{
computerWins = computerWins + 1;
}
// Display results and statistics
Console.WriteLine("'nYou have played {0} games with {1} wins, {2} draws, and {3} losses.", gamesPlayed, userWins, draws, computerWins);
do
{
Console.Write("Please make a selection: ");
// loop and test using TryParse()
while (!int.TryParse(Console.ReadLine(), out userSelection))
{
// invalid data type
Console.WriteLine("Invalid input.");
Console.Write("Please make a selection: ");
}
// test if input is within acceptable range
if (userSelection >= 1 && userSelection <= 4)
{
inputIsValid = true;
}
else
{
// valid integer, but out of acceptable range
Console.WriteLine("Number out of range.");
}
} while (!inputIsValid);
}
if (gamesPlayed == 0 && userSelection == 4)
{
Console.WriteLine("'nGoodbye");
}
else if (gamesPlayed > 0 && userSelection == 4)
{
Console.WriteLine("'nGames played = " + gamesPlayed);
Console.WriteLine("User wins = " + userWins);
Console.WriteLine("Computer wins = " + computerWins);
Console.WriteLine("Draws = " + draws);
}
Console.ReadLine();
}
}
}
如果您发现自己多次编写几乎相同的内容,您应该寻找重构它的方法。下面是一个不必要的重复代码的例子:
if (userSelection == 1)
{
Console.WriteLine("'nYou have selected Rock");
gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 2)
{
Console.WriteLine("'nYou have selected Paper");
gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 3)
{
Console.WriteLine("'nYou have selected Scissors");
gamesPlayed = gamesPlayed + 1;
}
可以简化为:
Console.WriteLine("'nYou have selected " + tools[userSelection - 1]);
gamesPlayed++;
其中工具定义为:
string[] tools = { "Rock", "Paper", "Scissors" };