错误CS0029:无法隐式转换类型“bool”;到 '(bool valorA, bool valorB)'时间:2019-03-17 标签:c#
我遇到了一个问题,我想制作一个 Switch Case 来说明谁赢了、谁输了或者他们是否平局(Venceu、Perdeu e Empatou)。
我首先使用整数值,使用 int valueA 和 valueB,其中如果有人获胜,则值 1,无论谁输了,都值 0 和平局,两者都值 1。
但是,它最终给出了错误:
错误 CS0029:无法将类型“bool”隐式转换为“(int valueA, int 值B)'
所以我使用布尔值进行了重新编辑,1 = true,0 = false。
但错误仍然存在,我不知道如何修复它。
这是我的代码:
if(Input.GetKey(key1)) // Vence
{
animBlue.SetBool("AzulTiro", true);
valorA = true;
}
else if(animRed.GetBool("VermelhoTiro") == true) // Perde
{
animBlue.SetBool("AzulMorte", true);
valorA = false;
}
if(Input.GetKey(key2)) // Vence
{
animRed.SetBool("VermelhoTiro", true);
valorB = true;
}
else if(animBlue.GetBool("AzulTiro") == true) // Perde
{
animRed.SetBool("VermelhoMorte", true);
valorB = false;
}
switch (valorA, valorB)
{
case (valorA == true && valorB == false):
Debug.Log("Azul Venceu");
break;
case (valorA == false && valorB == true):
Debug.Log("Vermelho Venceu");
break;
case (valorA == true && valorB == true):
Debug.Log("Empatou");
break;
}
我很确定错误出在 Switch 上,但我不知道它是什么
I'm having a problem, I want to make a Switch Case that says who won, who lost or if they drew ( Venceu, Perdeu e Empatou).
I did it first with integer values, with int valueA and valueB, where if someone wins it's worth 1, whoever loses it's 0 and a tie, both are worth 1.
However, it ended up giving the error:
error CS0029: Cannot implicitly convert type 'bool' to '(int valueA,
int valueB)'
So I redid using boolean values, 1 = true and 0 = false.
But the error still persists and I don't know how to fix it.
This is my code:
if(Input.GetKey(key1)) // Vence
{
animBlue.SetBool("AzulTiro", true);
valorA = true;
}
else if(animRed.GetBool("VermelhoTiro") == true) // Perde
{
animBlue.SetBool("AzulMorte", true);
valorA = false;
}
if(Input.GetKey(key2)) // Vence
{
animRed.SetBool("VermelhoTiro", true);
valorB = true;
}
else if(animBlue.GetBool("AzulTiro") == true) // Perde
{
animRed.SetBool("VermelhoMorte", true);
valorB = false;
}
switch (valorA, valorB)
{
case (valorA == true && valorB == false):
Debug.Log("Azul Venceu");
break;
case (valorA == false && valorB == true):
Debug.Log("Vermelho Venceu");
break;
case (valorA == true && valorB == true):
Debug.Log("Empatou");
break;
}
I'm pretty sure the error is on the Switch but I don't know what it is
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误的语法,您需要
wrong syntax, you need
如果您的 C# 足够现代,那么模式匹配怎么样?
我推断假/假要么不会发生,要么也是平局。如果不是,那么您可以调整该声明:
请记住,这是一个提供价值的声明,而不是一个采取行动的声明;如果编译器不详尽(未涵盖所有可能的输入值),您会收到编译器的投诉,最简单的方法是提供“else”情况
_
。另外,因为它提供值而不是执行代码,所以如果您要做出“做/不做”决定,您可能需要检查该值。您不能使用它来运行 void 的操作(假设 Debug.Log 返回 void),就像不能将 void 放入三元组中一样;三元数产生值:
但是,您可以使用的一个技巧是使用开关(或三元数)返回一个 lambda,然后执行该 lambda。
这些开关确实返回一个值;它们每个都返回一个 Action,然后可以调用它来执行日志记录。
它也可以在一行中形成和执行,尽管它开始有点“wtf?”:
How about pattern matching if your C# is modern enough?
I reasoned that a false/false would either not occur, or be a draw too. If it isn't then you can tweak the statement:
Remember that this a value-providing statement rather than an action-taking one; you'll get complaints from the compiler if it isn't exhaustive (doesn't cover all possible input values) and the easiest way to make it so is to provide the "else" case
_
. Also because it's providing values rather than performing code, you might need to inspect the value if you're making a "do/don't" decision.You can't use this to run actions that are void (assuming Debug.Log returns void) in the same way you can't put voids in a ternary; the ternary produces values:
One trick you can pull, however, is to use a switch (or a ternary) to return a lambda that you then execute
These switches do return a value; they each return an Action and later it can be invoked to carry out the logging
It can also be formed and executed in one line, though it starts to get a bit "wtf?":