首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用if C#执行3种条件

如何使用if C#执行3种条件
EN

Stack Overflow用户
提问于 2020-02-11 05:57:19
回答 3查看 285关注 0票数 0

我能够让C#读到我的两个条件。我能正确地得到答案:“你吹了10个口哨”和“平均工作蓝色,你的分数是18分”。

但我无法得到答案“出色的工作红色!你的分数是40",我的代码将错误地说”平均工作红色,你的分数是40分“。

请帮帮忙。我自己做了这个测试,以了解如何使用“如果是”来执行几个条件。

使用说明:

玩家的场景1:

  1. 问位
  2. 询问分数计数
  3. 问问哪个队。
  4. 说“普通的工作团队,你的分数是_”

玩家的场景2:

  1. 问位
  2. 问得分数。
  3. 问问哪个队。
  4. 如果得分超过20分,请说“优秀的工作团队!你的分数是_”。

裁判员方案3:

  1. 问位
  2. 询问口哨数
  3. 说“你吹口哨”

答案:

  • 职位:裁判员
  • 哨声总数: 10
  • “你吹了十个口哨”
  • 位置:球员
  • 得分: 18
  • 小组:蓝色
  • “平均工作是蓝色的,你的分数是18分”
  • 位置:球员
  • 得分: 40
  • 队:红队
  • “出色的工作红,你的分数是40分”
代码语言:javascript
复制
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();
EN

回答 3

Stack Overflow用户

发布于 2020-02-11 06:02:53

在用户输入scoreCount值之前,您的程序不知道该值。因此,如果position等于"Player",程序将始终进入第一个分支,因为scoreCount被初始化为0 (在比较时scoreCount < 20始终为真)。

在检查您的状况之前,尝试读取该值。

代码语言:javascript
复制
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;
    // ...
}

经过一点重构

代码语言:javascript
复制
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)。

票数 5
EN

Stack Overflow用户

发布于 2020-02-11 06:11:40

scoreCount值应该在If条件之外,而不是条件块内。检查以下代码:

代码语言:javascript
复制
        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,它将处于第一种情况下。

票数 1
EN

Stack Overflow用户

发布于 2020-02-11 06:32:21

代码语言:javascript
复制
            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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60162800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档