无法隐式转换类型';bool';到';int'?我该怎么解决这个问题

本文关键字:问题 解决 转换 类型 bool int | 更新日期: 2023-09-27 18:15:00

我正试图让用户输入两个变量,iFirst和iSecond,如果第一个大于第二个,则显示消息。如果iSecond等于iFirst显示消息,但如果iSeconds大于iFirst,则显示iThird。无论我尝试什么,它都会在代码的最后部分出现这个错误:(

有什么建议吗?

double int dFirst;
int iSecond;
int iThird;
Console.WriteLine("Enter a number betwen 1 and 10: ");
dFirst = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter another number between 1 and 10: ");
iSecond = Convert.ToInt32(Console.ReadLine());
if (dFirst > iSecond)
{
    Console.WriteLine("The first number is bigger than the second one you entered.");
}
else if (iSecond >= dFirst)
{
    Console.WriteLine("These numbers are equal");
}
else (iSecond >= dFirst)
{
    iThird = dFirst <= iSecond;
    Console.WriteLine("The number is: " + iThird);
    Console.WriteLine();
    Console.WriteLine("Press any key to close.");
    Console.ReadKey();
}

无法隐式转换类型';bool';到';int'?我该怎么解决这个问题

这个错误的意思正是它所说的——你试图给一个整数分配一个布尔值,这是没有意义的。true的整数值是多少?这就像是在问"这个橙色的苹果是什么品种的?"然而,你也有其他的虫子——看看我下面的评论。

double int dFirst;

没有"double int"这回事。double是实数,int是整数,显然一个数字不可能既是整数又是实数。

    int iSecond;
    int iThird;
    Console.WriteLine("Enter a number between 1 and 10: ");
    dFirst = Convert.ToInt32(Console.ReadLine());

如果他们输入的不是数字,这将导致程序崩溃。它也不会检查以确保用户输入了介于1和10之间的内容。尝试以下操作:

        int intResult;
        // Prompt the user for an input until they enter a number between 1 - 10
        while (!int.TryParse(Console.ReadLine(), out intResult) || intResult < 1 || intResult > 10)
        {
            Console.WriteLine("Not a valid int - try again");
        }

以下代码也有同样的问题:

    Console.WriteLine("Enter another number between 1 and 10: ");
    iSecond = Convert.ToInt32(Console.ReadLine());
    if (dFirst > iSecond)
    {
        Console.WriteLine("The first number is bigger than the second one you entered.");
    }
    else if (iSecond >= dFirst)
    {
        Console.WriteLine("These numbers are equal");

不,不一定-iSecond可以大于dFirst。我想你在这里指的是==

    }
    else (iSecond >= dFirst)

这应该是"else-if"或简单的"else"。在这种情况下,你可以只做"else-"——如果第一个不大于第二个,并且它们不相等,那么第二个显然必须更大。此外,这个条件与之前的条件完全相同,所以绝对不可能满足这个条件。考虑以下内容:

if (someCondition) {
   // ...
 }
// The following condition quite literally means "if not someCondition and someCondition," so obviously this couldn't possibly run.
else if (someCondition) { 
    // ...
 }

回想一下,"else-if"的意思是"如果前面的条件是false,我在这里指定的条件是true。">

            {
        iThird = dFirst <= iSecond;

这就是导致错误的原因。我想你的意思有点像:

iThird = dFirst <= iSecond ? iSecond : dFirst;

这会将较大的数字分配给iThird,但您可以随心所欲。

        Console.WriteLine("The number is: " + iThird);
        Console.WriteLine();
        Console.WriteLine("Press any key to close.");
        Console.ReadKey();
    }

您的问题在这里:

iThird = dFirst <= iSecond;

<=运算符产生布尔值(true或false(。试试这个:

iThird = dFirst <= iSecond ? (number if dFirst is less than or equal to iSecond) : (number if dFirst is greater than iSecond);

存在问题

 iThird = dFirst <= iSecond;

你到底想在这里做什么。您已经检查了第一个数字是否大于,两者是否相等,是否要检查第二个数字是否相等?如果stmt

,你能指定写第二个else背后的想法吗
 else (iSecond >= dFirst)
            {
        iThird = dFirst <= iSecond;

    }

这是你的错误。dFirst <= iSecond正在返回无法转换为int - iThird的布尔值。你需要做的是将的两个数字相加或相减

   else (iSecond >= dFirst)
            {
        iThird = dFirst + iSecond;

    }

编辑:在这个代码中还有许多其他错误

    int dFirst;
    int iSecond;
    int iThird;
    Console.WriteLine("Enter a number betwen 1 and 10: ");
    dFirst = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Enter another number between 1 and 10: ");
    iSecond = Convert.ToInt32(Console.ReadLine());
    if (dFirst > iSecond)
    {
        Console.WriteLine("The first number is bigger than the second one you entered.");
    }
    else if (iSecond >= dFirst)
    {
        Console.WriteLine("These numbers are equal");
    }
    else if(iSecond >= dFirst)
            {
        iThird = dFirst <= iSecond; //this is the line from the question
        Console.WriteLine("The number is: " + iThird);
        Console.WriteLine();
        Console.WriteLine("Press any key to close.");
        Console.ReadKey();
    }