C# 如何使用if-else C实现3个条件#
我可以让C#读取我的两个条件。我能正确地得到答案:“你吹了10个口哨”和“平均工作蓝,你的分数是18” 但是我不能得到答案“超级红作业!你的分数是40”,我的代码会错误地说“平均红作业,你的分数是40” 请帮忙。我自己做了这个测试,以了解如何使用if-else执行几个条件 说明: 玩家场景1:C# 如何使用if-else C实现3个条件#,c#,C#,我可以让C#读取我的两个条件。我能正确地得到答案:“你吹了10个口哨”和“平均工作蓝,你的分数是18” 但是我不能得到答案“超级红作业!你的分数是40”,我的代码会错误地说“平均红作业,你的分数是40” 请帮忙。我自己做了这个测试,以了解如何使用if-else执行几个条件 说明: 玩家场景1: 询问职位 询问分数计数 问哪个队 说“一般的工作团队,你的分数是u” 玩家场景2: 询问职位 询问分数计数 问哪个队 如果分数超过20,说“出色的工作团队!你的分数是u33;” 裁判员的情景3: 询问职位
- 位置:裁判
- 口哨总数:10
- “你吹了10个口哨”
- 位置:球员
- 计分:18分
- 队伍:蓝色
- “平均蓝色工作,你的分数是18”
- 位置:球员
- 分数:40
- 队伍:红色
- “干得好,红色,你的分数是40”
inttotalwhistles=0,scoreCount=0;
字符串位置、名称、团队;
控制台。写入(“输入位置:”);
位置=Console.ReadLine();
如果((位置=“玩家”| |位置=“玩家”)&&scoreCount<20)
{
控制台。写入(“输入分数计数:”);
scoreCount=int.Parse(Console.ReadLine());
控制台。写入(“输入团队:”;
team=Console.ReadLine();
WriteLine(“平均作业{0},您的分数为{1}”,团队,分数计数);
}
如果((位置=“球员”| |位置=“球员”)&&scoreCount>20)
{
控制台。写入(“输入分数计数:”;
scoreCount=int.Parse(Console.ReadLine());
控制台。写入(“输入团队:”;
team=Console.ReadLine();
WriteLine(“出色的工作{0}!您的分数计数为{1}”,团队,分数计数);
}
否则,如果(位置=“裁判员”|位置=“裁判员”)
{
控制台。写入(“输入总口哨:”;
totalWhistles=int.Parse(Console.ReadLine());
WriteLine(“你吹了{0}个口哨”,totalWhistles);
}
Console.ReadLine();
在用户输入值之前,您的程序不知道scoreCount
的值。因此,如果位置
等于“Player”
,您的程序将始终进入第一个分支,因为分数计数
初始化为0
(分数计数<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;
// ...
}
字符串位置、名称、团队;
控制台。写入(“输入位置:”);
位置=Console.ReadLine();
if(position.ToLower()=“player”)
{
积分计数=0;
控制台。写入(“输入分数计数:”);
scoreCount=int.Parse(Console.ReadLine());
控制台。写入(“输入团队:”;
team=Console.ReadLine();
如果(分数计数<20)
{
// ...
}
其他的
{
// ...
}
}
else if(position.ToLower()=“裁判员”)
{
int totalWhistles=0;
// ...
}
经过一点重构之后
public void ProcessPosition()
{
控制台。写入(“输入位置:”);
位置=Console.ReadLine();
if(IsPlayer(位置))
{
ProcessPlayer();
}
否则,如果(IsReferee(职位))
{
过程裁判();
}
Console.ReadLine();
}
私有bool IsPlayer(字符串位置)=>position.ToLower()=“播放器”;
私有布尔值是referee(字符串位置)=>position.ToLower()==“裁判员”;
私有进程播放器()
{
控制台。写入(“输入分数计数:”);
var scoreCount=int.Parse(Console.ReadLine());
控制台。写入(“输入团队:”;
var team=Console.ReadLine();
var消息=GetPlayerMessage(团队、计分计数);
控制台写入线(消息);
}
私有字符串GetPlayerMessage(字符串组,int scoreCount)=>scoreCount<20
? $“平均作业{team},您的分数为{scoreCount}”;
:$“出色的工作{team}!您的得分计数为{scoreCount}”;
请注意:我使用了而不是
if。。else
我发现它很有用。此外,我还用替换了对string.Format
的调用,因为这样可以减少字符串格式的混乱。在我看来,你在用过时的教程或书籍学习C#。我建议你马上学习现代C#。查看最新版本的综述和/或自己买一本新书(最好是C#8)。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);
}
Console.Write(“输入位置:”);
位置=Console.ReadLine();
//早些时候,这是在if条件内。
控制台。写入(“输入分数计数:”);
scoreCount=int.Parse(Console.ReadLine());
如果((位置=“玩家”| |位置=“玩家”)&&scoreCount<20)
{
控制台。写入(“输入团队:”;
team=Console.ReadLine();
//scoreCount=int.Parse(Console.ReadLine());从此处删除并添加到if条件之上。
WriteLine(“平均作业{0},您的分数为{1}”,团队,分数计数);
}
如果((位置=“球员”| |位置=“球员”)&&scoreCount>20)
{
控制台。写入(“输入团队:”;
team=Console.ReadLine();
WriteLine(“出色的工作{0}!您的分数计数为{1}”,团队,分数计数);
}
否则,如果(位置=“裁判员”|位置=“裁判员”)
{
控制台。写入(“输入总口哨:”;
totalWhistles=int.Parse(Console.ReadLine());
欺骗
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();