颜色游戏(计数循环未运行)

huangapple go评论73阅读模式
英文:

Color Game (Counter Loop not running)

问题

我一直在努力弄清楚为什么我的计数循环没有运行我有这段代码片段即使我满足了(counter>=10)的要求它也不会运行它应该运行那行表示我输掉了游戏的代码但无论如何都不会这样做起初我以为是因为计数器在循环内部由于它会继续进行所以永远不会满足条件但即使这样它仍然不会执行如果代码的间距不太好看我还是新手对编码不太熟悉

void draw ()
{
  int name=5, strikes=1;

  int [][] scores= new int [name][strikes];
  int score1=0;

  keyPressed();
  {
    for (int x=0; x<10; x++)
    {
      first();
      if (key == 'r' || key == 'R')
      {
        secoend();
      }

      if (key != 'r' || key != 'R')
      {
        score1++;
      } else if (score1>=10)
      {
        background(255);
        String text="You Lost the Game";
        text(text, 411, 90);
      }
    }
  }
}
英文:

I've been trying to figure out for the life of me why my counter loop isn't running. I have this snippet of my code that will not run even when I qualify the requirements of it being that (counter>=10). It should run the line saying that I lost the Game but it won't do so regardless. I initially thought it was because the counter was within the loop and since it'll keep going it won't ever meet the condition but even then it still won't go. My apologize if its not the best looking code far as spacing goes I'm still new to coding

void draw ()
{
  int name=5, strikes=1;

  int [][] scores= new int [name][strikes];
  int score1=0;

  keyPressed();
  {
    for (int x=0; x&lt;10; x++)
    {
      first();
      if (key == &#39;r&#39; || key == &#39;R&#39;)
      {
        secoend();
      }

      if (key != &#39;r&#39; || key != &#39;R&#39;)
      {
        score1++;
      } else if (score1&gt;=10)
      {
        background(255);
        String text=&quot;You Lost the Game&quot;;
        text(text, 411, 90);
      }
    }
  }
}

答案1

得分: 2

这个 if 语句总是为真,因此它永远不会进入 else 分支:

if (key != 'r' || key != 'R')

key 只能是一种情况,所以其中任何一个总是为真。

我认为你的意思是:

if (key != 'r' && key != 'R')
英文:

This if-statement is always true, and so it will never enter the else-branch:

 if (key != &#39;r&#39; || key != &#39;R&#39;)

key can only be one thing so either of those is always true

I think you ment:

if (key != &#39;r&#39; &amp;&amp; key != &#39;R&#39;)

答案2

得分: 1

检查一下你的if语句,你总是在递增score1,因为if(key != 'r' || key != 'R')将始终为真。

英文:

Check your if statements, you're always incrementing score1 because if(key != &#39;r&#39; || key != &#39;R&#39;) will always be true.

huangapple
  • 本文由 发表于 2020年9月29日 17:16:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64116498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定