无法将类型'bool'隐式转换为'int' [英] Cannot implicitly convert type 'bool' to 'int'
问题描述
嗨
我收到一个错误:
无法将类型"bool"隐式转换为"int"
无论如何,程序都会执行错误,但是我不知道如何解决.
这是代码段:
Hi
I get an error:
Cannot implicitly convert type ''bool'' to ''int''
anyway the program executes with the errors but I dont know how to fix it.
Here is the code snippet:
// switch on the value of inputInt
switch (inputInt)
{
case (inputInt == 0):
Console.WriteLine("your input is zero.");
goto repeat;
case (inputInt % 2 != 0):
Console.WriteLine("your input is odd.");
goto repeat;
case (inputInt % 2 == 0):
Console.WriteLine("your input is even.");
goto repeat;
case (inputInt % 10 == 0):
Console.WriteLine("your bla bla..");
goto repeat;
case (inputInt > 100):
Console.WriteLine("your input is bla bla..");
goto repeat;
}
我不明白这是什么问题.我们不能在案例陈述中加入公式和方程式吗?
最好的问候
这是学校的作业.
第一个任务是使用if-else语句执行此操作,没关系.
第二项任务是使用switch语句执行相同的程序...
这将是更多的代码,仅通过switch语句即可完成.
否则,我也不认为在这里使用switch语句会很有效.
任务本身:
1.创建一个程序来提示用户输入,接受一个整数,然后通过使用多个" if来评估该输入是零,奇数还是偶数,10的倍数或太大(大于100) "声明."
2.重写上一个练习中的程序,以使用switch语句完成相同的工作...
-------------------------------------------------- ---------------
为什么要嵌套它们?
我没有想到必须使用switch语句输出的方法.
例如10的倍数也是偶数.
所以必须要控制台输出会很高兴:
数字是10的倍数"
数字是偶数"
但是我今天真的很无聊,我怀疑只使用switch语句,甚至嵌套语句是不可能的.
I do not understand what is the problem. Can''t we have formulas and equations into the case statement?
Best regards
It is a schools assignment.
Task number one was to do this using if-else statements, it is ok.
Task number two is to do the same program, using a switch statement...
It is going to be more code, just to accomplish it with a switch statement.
Otherwise I also don''t thing it is efficient to use a switch statement here.
The tasks themselves:
1. "Create a program that prompts user for an input, accepts an integer, then evaluates whether that input is zero, odd or even, a multiple of 10, or too large (more than 100) by using multiple levels of ''if'' statements."
2."Rewrite the the program from the previous exercise to do the same work with a switch statement...
-----------------------------------------------------------------
why do you nest them?
i wast thinking of a way to have to outputs with a switch statement.
for example a number that is multiple of 10 is also an even number.
so it is gonna be nice to have to console outputs:
"the num is multiple of 10"
"the num is even"
but i got realy bored for today, and I suspect it is not possible only with a switch statements, even nested ones.
推荐答案
您的代码太不正确了!
如果inputInt
是int
,则case
值应为int
const
s,例如:0
,1
,10
...,而不是bool
(> false
)表达式.
Your code is soooo wrong!
IfinputInt
is anint
, then thecase
values should beint
const
s, ex:0
,1
,10
..., notbool
(true
offalse
) expressions.
Toli完全正确.告诉您的switch语句评估Int
.将开关视为更容易编写的If
Then
语句系列.在您的情况下,它等于:
Toli is exactly right. Your switch statement is told to evaluate anInt
. Think of the switch as an easier to write series ofIf
Then
statements. In your case, it would equate to:
if (inputInt == (inputInt == 0))
{
...;
}
else if (inputInt == (input %2 != 0))
{
...;
}
...
因此,它失败了,因为您正在询问它int
是否等于boolean
值.
来自msdn:
在C ++中,可以将bool类型的值转换为int类型的值;换句话说,false等于零,而true则等于非零值.在C#中,在bool类型和其他类型之间没有转换.例如,以下if语句在C#中无效:"
So, it fails because you''re asking it if an int
is equal to a boolean
value.
From msdn:
"In C++, a value of type bool can be converted to a value of type int; in other words, false is equivalent to zero and true is equivalent to nonzero values. In C#, there is no conversion between the bool type and other types. For example, the following if statement is invalid in C#:"
您的基本问题是您不了解switch语句. switch语句不是这种情况.要使用案例声明来做到这一点,您需要这样做:
Your basic issue is that you don''t understand switch statements. A switch statement is not a case. To do it with a case statement you''d need to do this:
switch (inputInt)
{
case 0:
Console.WriteLine("your input is zero.");
break;
case 2:
case 4:
case 6:
case 8: // etc
Console.WriteLine("your input is even.");
break;
//etc
我同意,这不是使用案例陈述的好地方,但这几乎肯定是您的老师希望看到的.要点:
1-case语句表示您列出要捕获的案例,而case并不是像您那样编写评估代码的地方
2-没有理由在这里或基本上在任何地方都使用goto.我从未在代码中使用过goto.它使您的代码不可读,也不必要.
3-您可以将一个以上的案例放在一个块中,所以我做了2,4,6,8等,然后将一个针对所有这些案例执行的代码块.
我应该添加几件事:
1-我们通常不做人的作业.我很高兴为您提供帮助,因为您正在尝试做自己的工作,而不仅仅是寻求解决方案,但在我看来,您似乎在努力处理案例陈述的工作方式,我建议您还可以与您的老师讨论此事,以确保您脑中清楚.
2-您不应该按下答案"按钮问更多问题.您应该编辑您的帖子(我已将您的回复复制到您的主要帖子中),或使用答案"部分下的论坛.
I agree, this is not a good place to use a case statement, but this is almost certainly what your teacher wants to see. The important points:
1 - a case statement means you list the cases you want to catch, a case is NOT a place to write evaluative code, as you did
2 - there is no reason to use goto here, or, basically, almost anywhere. I have never used goto in my code. It makes your code unreadable and is unnecessary.
3 - you can put more than one case in a block, so I did 2,4,6,8, etc, then put the one block of code that executes for all those cases.
I should add a couple of things:
1 - we don''t normally do people''s homework. I was happy to help you b/c you were trying to do your own work, not just asking for a solution, but it does seem to me like you were struggling a bit with the way case statements work, and I''d recommend you also talk to your teacher about this, to make sure it''s clear in your mind.
2 - you should not push the ''answer'' button to ask more questions. You should edit your post ( I copied your replies into your main post ), or use the forum under the answers section.
这篇关于无法将类型'bool'隐式转换为'int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!