我能够让C#读到我的两个条件。我能正确地得到答案:“你吹了10个口哨”和“平均工作蓝色,你的分数是18分”。
但我无法得到答案“出色的工作红色!你的分数是40",我的代码将错误地说”平均工作红色,你的分数是40分“。
请帮帮忙。我自己做了这个测试,以了解如何使用“如果是”来执行几个条件。
使用说明:
玩家的场景1:
玩家的场景2:
裁判员方案3:
答案:
int totalWhistles = 0, scoreCount = 0;
string position, name, team;
Console.Write("Enter position: ");
position = Console.ReadLine();
if ((position == "Player" || position == "player") && scoreCount < 20)
{
Console.Write("Enter the score count: ");
scoreCount = int.Parse(Console.ReadLine());
Console.Write("Enter team: ");
team = Console.ReadLine();
Console.WriteLine("Average job {0}, your score is {1}", team, scoreCount);
}
else if ((position == "Player" || position == "player") && scoreCount > 20)
{
Console.Write("Enter score count: ");
scoreCount = int.Parse(Console.ReadLine());
Console.Write("Enter team: ");
team = Console.ReadLine();
Console.WriteLine("Superb job {0}! Your score count is {1}", team, scoreCount);
}
else if (position == "referee" || position == "Referee")
{
Console.Write("Enter total whistles: ");
totalWhistles = int.Parse(Console.ReadLine());
Console.WriteLine("You blowed {0} whistles", totalWhistles);
}
Console.ReadLine();发布于 2020-02-11 06:02:53
在用户输入scoreCount值之前,您的程序不知道该值。因此,如果position等于"Player",程序将始终进入第一个分支,因为scoreCount被初始化为0 (在比较时scoreCount < 20始终为真)。
在检查您的状况之前,尝试读取该值。
string position, name, team;
Console.Write("Enter position: ");
position = Console.ReadLine();
if(position.ToLower() == "player")
{
int scoreCount = 0;
Console.Write("Enter the score count: ");
scoreCount = int.Parse(Console.ReadLine());
Console.Write("Enter team: ");
team = Console.ReadLine();
if(scoreCount < 20)
{
// ...
}
else
{
// ...
}
}
else if (position.ToLower() == "referee")
{
int totalWhistles = 0;
// ...
}经过一点重构
public void ProcessPosition()
{
Console.Write("Enter position: ");
position = Console.ReadLine();
if(IsPlayer(position))
{
ProcessPlayer();
}
else if (IsReferee(position))
{
ProcessReferee();
}
Console.ReadLine();
}
private bool IsPlayer(string position) => position.ToLower() == "player";
private bool IsReferee(string position) => position.ToLower() == "referee";
private ProcessPlayer()
{
Console.Write("Enter the score count: ");
var scoreCount = int.Parse(Console.ReadLine());
Console.Write("Enter team: ");
var team = Console.ReadLine();
var message = GetPlayerMessage(team, scoreCount);
Console.WriteLine(message);
}
private string GetPlayerMessage(string team, int scoreCount) => scoreCount < 20
? $"Average job {team}, your score is {scoreCount}";
: $"Superb job {team}! Your score count is {scoreCount}";请注意:,我用的是操作员)而不是if .. else,我觉得它很有用。此外,我已经将string.Format的调用替换为串内插,因为这允许以较少杂乱的方式格式化字符串。在我看来,你是用过时的教程或书籍来学习C#的。我建议你马上学习现代C#。看看最新版本的综述,或者给自己买一本最近的书(最好是C#8)。
发布于 2020-02-11 06:11:40
scoreCount值应该在If条件之外,而不是条件块内。检查以下代码:
Console.Write("Enter position: ");
position = Console.ReadLine();
// earlier this was inside the if condition.
Console.Write("Enter the score count: ");
scoreCount = int.Parse(Console.ReadLine());
if ((position == "Player" || position == "player") && scoreCount < 20)
{
Console.Write("Enter team: ");
team = Console.ReadLine();
//scoreCount = int.Parse(Console.ReadLine()); removed from here added on top of if conditon.
Console.WriteLine("Average job {0}, your score is {1}", team, scoreCount);
}
else if ((position == "Player" || position == "player") && scoreCount > 20)
{
Console.Write("Enter team: ");
team = Console.ReadLine();
Console.WriteLine("Superb job {0}! Your score count is {1}", team, scoreCount);
}
else if (position == "referee" || position == "Referee")
{
Console.Write("Enter total whistles: ");
totalWhistles = int.Parse(Console.ReadLine());
Console.WriteLine("You blowed {0} whistles", totalWhistles);
}始终您的scoreCount为0,如果条件仅为0,它将处于第一种情况下。
发布于 2020-02-11 06:32:21
int totalWhistles = 0, scoreCount = 0;
string position = "", team = "";
Console.Write("Enter position: ");
position = Console.ReadLine();
if ((position.ToLower() == "player" ))
{
Console.Write("Enter the score count: ");
scoreCount = int.Parse(Console.ReadLine());
Console.Write("Enter team: ");
team = Console.ReadLine();
if(scoreCount > 20)
Console.WriteLine("Superb job {0}! Your score count is {1}", team, scoreCount);
else
Console.WriteLine("Average job {0}! Your score count is {1}", team, scoreCount);
}
else if (position.ToLower() == "referee")
{
Console.Write("Enter total whistles: ");
totalWhistles = int.Parse(Console.ReadLine());
Console.WriteLine("You blowed {0} whistles", totalWhistles);
}
Console.ReadLine();https://stackoverflow.com/questions/60162800
复制相似问题