这是我的代码。我想让它工作,如果用户输入 y 它将继续循环,否则如果他们按 q 程序退出。我看到很多人使用标志,我认为这很棒,所以我尝试做一些事情,但我不知道为什么它不起作用。我尝试使用中断;并继续;但输出仍然不是我想要的。
#include <stdio.h>
int main(){
int broj1,broj2,zbir;
char exit;
int flag = 0;
while(!flag){
printf("Enter numbers: \n");
scanf("%d %d",&broj1,&broj2);
zbir = broj1+broj2;
printf("N1 + N2 is: %d\n",zbir);
printf("Type 'q' if you want to exit or 'y' to continue: ");
scanf("%c",&exit);
if(exit=='q')
flag=1;
if(exit=='y')
flag=0;
}
return 0;
}
最佳答案
你可以使用 exit = getch();
,而不是 scanf,而且如果你使用 getch() ,用户不需要在输入 y 或 q 后按 Enter 键,如果你愿意的话要保留你的 scanf,你可以在它之前添加 fflush(stdin);
(从上次 scanf 调用中删除 Enter)
关于c - 旗帜如何发挥作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59097350/