做一会儿没用 [英] do-while did not work

查看:88
本文介绍了做一会儿没用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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.


这篇关于做一会儿没用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆