C# 无法隐式转换类型';布尔';至';int'?我该如何解决这个问题?
我试图让用户输入两个变量,iFirst和iSecond,如果第一个比第二个大,则显示消息。如果iSecond等于iFirst显示消息,但如果iSecond大于iFirst,则显示iHird。无论我尝试了什么,它都会在代码的末尾出现这个错误:( 有什么建议吗C# 无法隐式转换类型';布尔';至';int'?我该如何解决这个问题?,c#,C#,我试图让用户输入两个变量,iFirst和iSecond,如果第一个比第二个大,则显示消息。如果iSecond等于iFirst显示消息,但如果iSecond大于iFirst,则显示iHird。无论我尝试了什么,它都会在代码的末尾出现这个错误:( 有什么建议吗 double int dFirst; int iSecond; int iThird; Console.WriteLine("Enter a number betwen 1 and 10: "); dFirst = Convert.ToIn
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();
}
double-int-dFirst;
等秒;
内部集成;
WriteLine(“输入一个介于1和10之间的数字:”);
dFirst=Convert.ToInt32(Console.ReadLine());
WriteLine(“输入另一个介于1和10之间的数字:”);
iSecond=Convert.ToInt32(Console.ReadLine());
如果(dFirst>iSecond)
{
WriteLine(“第一个数字大于您输入的第二个数字。”);
}
否则如果(iSecond>=dFirst)
{
Console.WriteLine(“这些数字相等”);
}
else(iSecond>=dFirst)
{
i第一个问题是:
iThird = dFirst <= iSecond;
iThird=dFirst
编辑:此代码中还有许多其他错误
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();
}
intdfirst;
等秒;
内部集成;
WriteLine(“输入一个介于1和10之间的数字:”);
dFirst=Convert.ToInt32(Console.ReadLine());
WriteLine(“输入另一个介于1和10之间的数字:”);
iSecond=Convert.ToInt32(Console.ReadLine());
如果(dFirst>iSecond)
{
WriteLine(“第一个数字大于您输入的第二个数字。”);
}
否则如果(iSecond>=dFirst)
{
Console.WriteLine(“这些数字相等”);
}
否则如果(iSecond>=dFirst)
{
i第一个问题是
iThird = dFirst <= iSecond;
iThird=dFirst这个错误的意思就是它所说的-你试图将一个布尔值赋给一个整数,这是没有意义的。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");
}
if (someCondition) {
// ...
}
// The following condition quite literally means "if not someCondition and someCondition," so obviously this couldn't possibly run.
else if (someCondition) {
// ...
}
不,不一定-iSecond可能大于dFirst。我猜您在这里指的是=
}
else (iSecond >= dFirst)
这应该是“elseif”或简单的“else”。在这种情况下,您可以只做“else”-如果第一个不大于第二个,它们不相等,那么显然第二个必须更大。而且,这个条件与前面的条件完全相同,所以绝对不能满足这种条件。
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");
}
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。”
3个问题:A.双整数dFirst;
是什么?应该是双整数
或整数
,但不能两者都是。B.还有其他
不能作为条件:其他(iSecond>=dFirst)
如果
或者没有条件,那么应该是else.C.对于问题的一部分:i第一个问题是微笑,第二个问题也是,除了我上面的评论之外-而且你永远不会到达else
-检查前的条件是第一个>第二个还是第二个>=第一个-还有什么其他问题“iSecond>=dFirst”也是错误的,因为他声称这个条件意味着iSecond和dFirst相等,这不一定是真的。这仍然不会编译。这是问题的答案。我应该在答案中修复整个代码吗?@M B-我不是说你应该做什么:)我只是说这个代码示例仍然无效并且是“usful”答案是可行的。我不是来自快速下降的选民,而是一些人are@MB@GiladGreen的意思是,你不能添加int
和double
并将结果double
分配给int
变量。他的问题之一是:)@EJoshuaS我指的是问题所指的问题特别是错误信息。公平地说,这就是他问的问题…只是指出代码中还有其他问题,所以OP不认为修复会自动使程序正常工作。
}
else (iSecond >= dFirst)
if (someCondition) {
// ...
}
// The following condition quite literally means "if not someCondition and someCondition," so obviously this couldn't possibly run.
else if (someCondition) {
// ...
}
{
iThird = dFirst <= iSecond;
iThird = dFirst <= iSecond ? iSecond : dFirst;
Console.WriteLine("The number is: " + iThird);
Console.WriteLine();
Console.WriteLine("Press any key to close.");
Console.ReadKey();
}