我怎样才能在布尔值中生成int命令?

时间:2017-10-04 12:18:48

标签: c#

我想和我在这里做的一样,做同样的操作,但是我没有使用int first_dig和second_dig,而是想用布尔值,例如:bool check = (new code here);

           Console.Write("Enter a two digit number: ");
            int two_dig_num = int.Parse(Console.ReadLine());
            if (two_dig_num >= 10 && two_dig_num <= 99)
            {
                int first_dig = two_dig_num % 10;
                int second_dig = two_dig_num / 10;

                if (first_dig == second_dig)
                    Console.WriteLine("YES!");

                else
                    Console.WriteLine("NO...");
            }
            else
                Console.WriteLine("\nYou haven't entered a Two Digit Number,\nPlease exit the program and try again later");
                return;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

first_dig == second_dig会返回 bool ,因此如果您想将其存储在某个变量中,那么只需:

bool value = first_dig == second_dig;

或使用较少的变量,它可以是:

bool value = two_dig_num % 10 == two_dig_num / 10;

就是这样。