我目前正在尝试自学C,因为我认为这将是C ++和C#的良好选择(以及在上课之前获得先机)。因此,我决定在此处编写此循环:
I am currently trying to teach myself C since I believe that will be a good segue into C++ and C# (as well as getting a headstart prior to the start of classes). So I have decided to write this loop here:
#include <stdio.h> int main() { bool continueLoop = true; char response; printf("ARE YOU READY TO RUMBLE?!?!\n Y/N\n"); response = getchar(); int counter = 0; do { counter++; if (response == 'Y') { printf("AWESOME!"); continueLoop == false; return 0; } else if (response == 'N') { printf("YOU FAIL!"); continueLoop == false; return 0; } if (continueLoop == true) { printf("I do not understand your input!\n"); printf("Please reinput! Y/N\n"); response = getchar(); } if(counter == 5) { printf("Exiting!"); continueLoop == false; goto exit; } }while (continueLoop == true); exit: return 0; }我的问题如下:为什么在什么时候如果我输入例如 M作为答案,它将自我循环两次;但是,如果给出适当的条件,则可以正确终止。
My question(s) is(are) as follows: Why is it when if I input for example 'M' as my answer, it will loop itself twice; however if it is given the proper condition then terminates correctly.
此外,我应该将响应转换为单个长度的数组,而不是 getchar()比较它,或者应该通过printf语句来完成它,这样 printf(您准备朗读了吗?!?n%s,响应);
Also, instead of getchar() should I instead convert response into a single length of array and then try to compare it somehow, or perhaps it should be done via the printf statement as so printf("ARE YOU READY TO RUMBLE?!? \n %s", response);
如果有帮助,我将C-Lion作为我的IDE,因为我拒绝在vi,emacs或记事本中编写任何代码。
If it helps, I am using C-Lion as my IDE as I refuse to write any code in vi, emacs, or notepad.
编辑代码
int main() { char response; printf("ARE YOU READY TO RUMBLE?!?!\n Y/N\n"); scanf(" %c", &response); int counter = 0; while (counter < 5) { counter++; if (response == 'Y') { printf("AWESOME!"); return 0; } else if (response == 'N') { printf("YOU FAIL!"); return 0; } else { printf("I do not understand your input!\n"); printf("Please reinput! Y/N\n"); response = getchar(); } } return 0; }推荐答案
当您输入 M 时,您实际上并不是仅输入 M ;您输入的是 M ,然后输入换行符。因此, getchar(3)首次调用时将返回 M ,然后返回 \n 。因此循环执行两次。
When you input M, you're not really inputting M only; you are inputting M followed by a newline. So getchar(3) will return M when you first call it, and then it will return \n on the second call. Hence the loop executes twice.
您可以使用 scanf(%c捕获输入并忽略空格(换行符,制表符,空格等)。 ,& response)。注意格式字符串中的前导空格;必须强制 scanf(3)跳过空格。
You can capture input and ignore whitespaces (newlines, tabs, spaces, etc) with scanf(" %c", &response). Note the leading whitespace in the format string; it is required to force scanf(3) to skip blanks.
此外,前两个if中的这些语句是无用的:
Also these statements inside the first two if's are useless:
continueLoop == false;您将 continueLoop 与 false ,然后丢弃结果(如果您未使用 -Wall 进行编译,则应该这样做,因为这很可能会给您一个警告)。
You compare continueLoop to false and then throw the result away (if you're not compiling with -Wall, you should because this will very likely give you a warning).
您可能想要分配而不是比较:
You probably want assignment instead of comparison:
continueLoop = false;更多推荐
循环重复本身?
发布评论