做一会儿没用

编程入门 行业动态 更新时间:2024-10-26 01:28:20
本文介绍了做一会儿没用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

#include <stdio.h> #include <stdlib.h> int main(void) { char again; do{ printf("insert y or Y to repeat"); fflush(stdout); scanf("%c",&again); }while(again=='y'||again=='Y'); }

我编写了这段代码以创建一个循环,当再次插入y或Y时,它会再次执行此工作,但是没有用.当我第一次输入y时,循环结束. [edit]代码块已修复[/edit]

i wrote this code to create a loop that when insert y or Y do the job again but did not work.when i enter first y the loop be end. [edit]code blocks fixed[/edit]

推荐答案

您按了y;和"\ n"(输入).两个字符. 您可以对其进行修改 you pressed ''y; and ''\n''(Enter).two chars. you can modify it #include <stdio.h> #include <stdlib.h> int main(void) { char again; do{ printf("insert y or Y to repeat"); fflush(stdout); scanf("%c",&again); getchar(); }while(again=='y'||again=='Y'); }

我工作.但这并不像您想象的那样起作用. 作为一种简单的解决方法,您可以使用字符串而不是char作为scanf的输入,例如 I works. But it doesn''t work the way you think it does. As a simple workaround you may use a string instead of char as input for scanf, e.g. #include <stdio.h> #include <stdlib.h> int main(void) { char buf[0x100]; do{ printf("insert y or Y to repeat"); fflush(stdout); while (scanf("%s",buf) != 1); }while(buf[0]=='y'|| buf[0]=='Y'); }

[更新] 试试这个:

[Update] try this one:

#include <stdio.h> #include <stdlib.h> int scan_frac(int *num,int *denom); int main(void){ int num1,denom1; char buf[0x100]; do{ if ( scan_frac(&num1,&denom1) == 2) { if ( denom1 != 0) { printf("%d/%d = %g\n", num1, denom1, ((double)num1)/denom1); fflush(stdout); } } printf("insert (y) to do another division\n"); fflush(stdout); scanf("%s", buf); }while(buf[0]=='y'||buf[0]=='Y'); return 0; } int scan_frac(int *num,int *denom){ int values; int n,dn; printf("insert numerator and denominator (use blank as separator): "); fflush(stdout); values = scanf("%d %d",&n,&dn); *num=n; *denom=dn; return values; }

[/Update]

[/Update]

您应该已经从回答您的其他问题中注意到了. 只问一次您的问题. 这样的交叉发布只会惹恼那些本来可以帮助您的人. You should have noticed from the replies to you other questions. Only ask your question ONCE. Cross-posting like this will only annoy those people who might otherwise help you.

更多推荐

做一会儿没用

本文发布于:2023-05-31 10:07:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会儿   没用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!