C语言 中断并继续 (C)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31373376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:02:37  来源:igfitidea点击:

Break and Continue (C)

cbreak

提问by Sentient

"Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence. Create a for loop that compares the two strings starting from index 0. For each match, add one point to userScore. Upon a mismatch, exit the loop using a break statement.

Ex: The following patterns yield a userScore of 4:

simonPattern: R, R, G, B, R, Y, Y, B, G, Y

userPattern: R, R, G, B, B, R, Y, B, G, Y

“西蒙说”是一种记忆游戏,其中“西蒙”输出 10 个字符(R、G、B、Y)的序列,用户必须重复该序列。创建一个 for 循环,比较从索引 0 开始的两个字符串。对于每个匹配项,向 userScore 添加一个点。在不匹配时,使用 break 语句退出循环。

例如:以下模式产生的 userScore 为 4:

西蒙模式:R、R、G、B、R、Y、Y、B、G、Y

用户模式:R、R、G、B、B、R、Y、B、G、Y

#include <stdio.h>
#include <string.h>

int main(void) {
   char simonPattern[50] = "";
   char userPattern[50] = "";
   int userScore = 0;
   int i = 0;

   userScore = 0;
   strcpy(simonPattern, "RRGBRYYBGY");
   strcpy(userPattern, "RRGBBRYBGY");

   while (userPattern[i] = simonPattern[i]) {
      userScore = userScore + 1;
      ++i;
      if (userPattern[i] != simonPattern[i]) {
         break;
      }
   }

   printf("userScore: %d\n", userScore);

   return 0;
}

I tried running the code, but I got this

我尝试运行代码,但我得到了这个

http://i.imgur.com/T7srTbb.png

http://i.imgur.com/T7srTbb.png

Does anyone know what's causing the extra 1?

有谁知道是什么导致了额外的1?

Thanks.

谢谢。

回答by Neil Locketz

Change the condition in your while loop to use ==not =. Right now you are making an assignment and not comparing the two meaning the first chars will always be the same and give a score of 1. You can then remove the if statement from inside the loop.

将 while 循环中的条件更改为使用==not =。现在你正在做一个分配而不是比较这两个字符,这意味着第一个字符将始终相同并给出 1 分。然后你可以从循环内部删除 if 语句。

The assignment asks you to use a for loop and not a while. If you just move I into the for loop and fill out the rest, along with moving the increment to after the != check it would work.

作业要求您使用 for 循环而不是 while。如果您只是将 I 移入 for 循环并填写其余部分,同时将增量移动到 != 之后,请检查它会起作用。

回答by Appcoder

while (simonPattern.at(i) == userPattern.at(i))
{
++i;
++userScore;
}

回答by POTATO_IN_MY_ANUS

Be sure to use a "FOR" loop, I see that you're using a "WHILE" loop. You will want to use the == comparative operator as opposed to the = assignment operator.

请务必使用“FOR”循环,我看到您正在使用“WHILE”循环。您将需要使用 == 比较运算符而不是 = 赋值运算符。

Here's my code and it worked out fine:

这是我的代码,效果很好:

#include <stdio.h>
#include <string.h>

int main(void) {
   char simonPattern[50];
   char userPattern[50];
   int userScore;
   int i;

   userScore = 0;
   strcpy(simonPattern, "RRGBRYYBGY");
   strcpy(userPattern, "RRGBBRYBGY");

   i = 0;

   for ( i = 0; userPattern[i] == simonPattern[i]; ++i) {
      userScore = userScore +1;
      if (userPattern[i] != simonPattern[i]){
         break;
      }
   }


   printf("userScore: %d\n", userScore);

   return 0;
}