【问题标题】:Condition ? : operator in C#健康)状况 ? : C# 中的运算符
【发布时间】:2020-08-22 04:54:09
【问题描述】:

?: 运算符有问题,所以我尝试了 if-else 块,一切正常。但是当使用 other 运算符时,它就停止工作了。

using System;

namespace firstGame
{
    class Program 
    {
        public string playerName;
        public int playerScore;
        public int gameNumber;
        public int playerGuess;

        public void GameStart()
        {
            string r;
            Console.WriteLine("Welcome to my first game");
            Console.Write("please enter your gamer name : ");

            this.playerName= Console.ReadLine();

            do
            {    
                this.gameNumber = Convert.ToInt16(new Random().Next(0,10));
                this.playerScore = 1;

                if (this.playerScore == 1)
                {
                    Console.WriteLine("Guess the hidden number between (1-10)");

                    do
                    {
                        Console.Write("guess number {0} :: ", this.playerScore);
                        string num = Console.ReadLine();

                        int.TryParse(num, out _) ? this.playerGuess=Convert.ToInt16(num) : Console.WriteLine("Are you sure it is a number !!?") ;
                        this.playerScore++;
                    } while (this.playerGuess != this.gameNumber);

                    Console.WriteLine("BINGO {0} is the correct number", this.gameNumber);
                    Console.Write("would you like a new lvl ? (Y/N) :: ");  r=Console.ReadLine();
                }
                else 
                { 
                    this.playerScore = 0; 
                    break; 
                }
            } while (r=="Y");
        }

        static void Main(string[] args)
        {
            new Program().GameStart();
        }
    }
}

我在某处犯了一个错误,但在哪里如何超出了我的范围。

【问题讨论】:

    标签: c# conditional-operator


    【解决方案1】:

    ?: 不是 if then else

    The conditional operator

    条件运算符?:,也称为三元条件 运算符,计算一个布尔表达式并返回一个结果 两个表达式,取决于布尔表达式是否 计算结果为真或假

    此外,Console.WriteLine 不返回结果,它是一个具有副作用的 void 方法。所以它不能与运算符一起使用,句号。

    简而言之,只需使用 if then else

    【讨论】:

      【解决方案2】:

      使用cond ? exp1 : exp2 时,exp1exp2 必须具有相同的类型。

      三元表达式不适用于您的情况,因为 Console.WriteLine 返回 void 但赋值表达式是 int

      改用传统的if-else

      修复

      // ERROR: This doesn't compile
      int.TryParse(num, out _) ? this.playerGuess=Convert.ToInt16(num) : Console.WriteLine("ar you sure it is a number !!?") ;
      

      应该变成

      if (int.TryParse(num, out _)) {
          this.playerGuess = Convert.ToInt16(num)
      } else {
          Console.WriteLine("ar you sure it is a number !!?");
      }
      

      额外提示:

      你可能真的想要这个:

      if (int.TryParse(num, out int val)) {
          this.playerGuess = val;
      } else {
          Console.WriteLine("Are you sure it is a number?");
      }
      

      【讨论】:

      • 额外提示,他们可能真的想要while(!int.TryParse(num, out playerGuess)) {Console.Write("are you sure it is a number?");mum=Console.Readline();}
      • (双重奖励提示,编写一个执行写入/读取的 Ask 方法,然后是一个使用它并在返回之前坚持一个数字的 AskInt 方法,通常会大量清理那些编程 101 应用程序!)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      相关资源
      最近更新 更多