【问题标题】:Stuck with exiting from a loop in C卡在退出 C 中的循环
【发布时间】:2012-03-03 21:28:19
【问题描述】:

问题是我不知道如何退出循环。

printf ("Do you order fish? (Y/N): ");
scanf  ("%c", &f);

while((f == 'y')||(f == 'Y'))
{
  do {
    fish = getfish_choice();
    printf ("Total of you fish is %.2lf\n", sum);
    printf ("Do you want to order more fish?(Y/N)");
    scanf  (" %c", &morefish);
  }
  while ((morefish=='Y')||(morefish=='y'));
}

Printf ("Hello");

我怎样才能退出这个循环,这样我的代码输出就会显示 Hello?

【问题讨论】:

    标签: c loops while-loop


    【解决方案1】:

    您有一个while 循环,其条件涉及检查f。但你永远不会修改f

    所以也许第一个while 实际上应该是if

    【讨论】:

      【解决方案2】:

      将外部 while 循环更改为 if: if(f == 'y'||f == 'Y')

      【讨论】:

        【解决方案3】:

        发生的情况是,当输入Yy 时,您设法跳出内循环,但仍卡在外循环中。 (f 永远不会被修改)

        你甚至不需要两个循环:

        printf ("Do you order fish? (Y/N): ");
        scanf  ("%c", &morefish);
        
        while ((morefish=='Y')||(morefish=='y')){
            fish = getfish_choice();
            printf ("Total of you fish is %.2lf\n", sum);
            printf ("Do you want to order more fish?(Y/N)");
            scanf  (" %c", &morefish);
        }
        
        printf ("Hello");
        

        【讨论】:

          猜你喜欢
          • 2010-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多