英文:
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<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);
}
}
}
}
答案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 != 'r' || key != 'R')
key
can only be one thing so either of those is always true
I think you ment:
if (key != 'r' && key != 'R')
答案2
得分: 1
检查一下你的if语句,你总是在递增score1,因为if(key != 'r' || key != 'R')
将始终为真。
英文:
Check your if statements, you're always incrementing score1 because if(key != 'r' || key != 'R')
will always be true.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论