错误CS0029:无法隐式转换类型“bool”;到 '(bool valorA, bool valorB)'时间:2019-03-17 标签:c#

发布于 2025-01-20 23:29:16 字数 1441 浏览 1 评论 0原文

我遇到了一个问题,我想制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2025-01-27 23:29:16

错误的语法,您需要

  switch (valorA, valorB)
    {
        case (true ,false):
              Debug.Log("Azul Venceu");
              break;
        
        case (false , true):
              Debug.Log("Vermelho Venceu");
              break;

        case (true, true):
              Debug.Log("Empatou");
              break;
    }

wrong syntax, you need

  switch (valorA, valorB)
    {
        case (true ,false):
              Debug.Log("Azul Venceu");
              break;
        
        case (false , true):
              Debug.Log("Vermelho Venceu");
              break;

        case (true, true):
              Debug.Log("Empatou");
              break;
    }
无敌元气妹 2025-01-27 23:29:16

如果您的 C# 足够现代,那么模式匹配怎么样?

var msg = (valorA, valorB) switch {
  (true, false) => "Azul Venceu",
  (false, true) => "Vermelho Venceu",
  _ => "Empatou"
};

Debug.Log(msg);

我推断假/假要么不会发生,要么也是平局。如果不是,那么您可以调整该声明:

var msg = (valorA, valorB) switch {
  (true, false) => "Azul Venceu",
  (false, true) => "Vermelho Venceu",
  (false, true) => "Empatou",
  _ => null
};

if(msg != null) Debug...

请记住,这是一个提供价值的声明,而不是一个采取行动的声明;如果编译器不详尽(未涵盖所有可能的输入值),您会收到编译器的投诉,最简单的方法是提供“else”情况_。另外,因为它提供值而不是执行代码,所以如果您要做出“做/不做”决定,您可能需要检查该值。

您不能使用它来运行 void 的操作(假设 Debug.Log 返回 void),就像不能将 void 放入三元组中一样;三元数产生值:

//no
someBoolean? Debug.Log("yes") : Debug.Log ("no");

//yes
Debug.Log(someBoolean? "yes" : "no");

但是,您可以使用的一个技巧是使用开关(或三元数)返回一个 lambda,然后执行该 lambda。

Action a = (valorA, valorB) switch {
  (true, false) => () => Debug.Log("Azul Venceu"),
  (false, true) => () => Debug.Log("Vermelho Venceu"),
  (true, true) => () => Debug.Log("Empatou")
  _ => () => {}
};

a(); //execute the returned action, which may be the do-nothing action 

这些开关确实返回一个值;它们每个都返回一个 Action,然后可以调用它来执行日志记录。

它也可以在一行中形成和执行,尽管它开始有点“wtf?”:

((valorA, valorB) switch {
  (true, false) => (Action)(() => Debug.Log("Azul Venceu")),
  (false, true) => () => Debug.Log("Vermelho Venceu"),
  (true, true) => () => Debug.Log("Empatou")
  _ => () => {}
})();

How about pattern matching if your C# is modern enough?

var msg = (valorA, valorB) switch {
  (true, false) => "Azul Venceu",
  (false, true) => "Vermelho Venceu",
  _ => "Empatou"
};

Debug.Log(msg);

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:

var msg = (valorA, valorB) switch {
  (true, false) => "Azul Venceu",
  (false, true) => "Vermelho Venceu",
  (false, true) => "Empatou",
  _ => null
};

if(msg != null) Debug...

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:

//no
someBoolean? Debug.Log("yes") : Debug.Log ("no");

//yes
Debug.Log(someBoolean? "yes" : "no");

One trick you can pull, however, is to use a switch (or a ternary) to return a lambda that you then execute

Action a = (valorA, valorB) switch {
  (true, false) => () => Debug.Log("Azul Venceu"),
  (false, true) => () => Debug.Log("Vermelho Venceu"),
  (true, true) => () => Debug.Log("Empatou")
  _ => () => {}
};

a(); //execute the returned action, which may be the do-nothing action 

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?":

((valorA, valorB) switch {
  (true, false) => (Action)(() => Debug.Log("Azul Venceu")),
  (false, true) => () => Debug.Log("Vermelho Venceu"),
  (true, true) => () => Debug.Log("Empatou")
  _ => () => {}
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文