简体   繁体   English

我在 C 中的 while 循环在结束前多重放了一次

[英]My while loop in C replays one more time than it should before it ends

When given input, my while loops print one time extra before closing its loop.当给定输入时,我的 while 循环在关闭循环之前额外打印一次。 For example, 8 7 38 3 -5 -1 q is my input that prints out例如, 8 7 38 3 -5 -1 q 是我打印出来的输入

Provide 6 integer coefficients for the brute force equation solver: Solution found: x = 3, y = 2为强力方程求解器提供 6 个 integer 系数: 找到的解:x = 3, y = 2

Run program again?再次运行程序? Type 'q' to quit or any other key to continue:输入“q”退出或任何其他键继续:

Provide 6 integer coefficients for the brute force equation solver: Solution found: x = 3, y = 2为强力方程求解器提供 6 个 integer 系数: 找到的解:x = 3, y = 2

Run program again?再次运行程序? Type 'q' to quit or any other key to continue:输入“q”退出或任何其他键继续:

When it should end after the first iteration.第一次迭代后应该何时结束。 Can anyone help me out on this?谁能帮我解决这个问题? My code is pasted below我的代码粘贴在下面

#include <stdio.h>
#include <math.h>
#include <stdbool.h>

int main(void) 
{
   //Equations should be a1x + b1y = c2 and a2x + b2y = c2
   int a1, b1, c1, a2, b2, c2, x, y;
   char input;
   bool solFound = false;
   bool runAgain = true;
   
   //brute force x and y [-10, 10]
   while (runAgain == true) 
   {
      printf("Provide 6 integer coefficients for the brute force equation solver: ");
      scanf("%d %d %d %d %d %d", &a1, &b1, &c1, &a2, &b2, &c2);
      for (x = -10; x<=10; x++)
      {
         for (y = -10; y<=10; y++)
         {
            if (a1*x + b1*y == c1 && a2*x + b2*y == c2)
            {
                  printf("\nSolution found: x = %d, y = %d\n\n", x, y);
                  solFound = true;
                  runAgain = false;
            }
         }
      }
      if (solFound != true)
      {
         printf("No solution found\n\n");
         runAgain = false;
      }
      scanf("%c", &input);
      printf("Run program again? Type 'q' to quit or any other key to continue:");
      if (input != 'q')
      {
         runAgain = true;
      }
      printf("\n\n");
   }
} ```

For starters the variable sqlFound should be declared within the while loop or at least reinitialized within the while loop.对于初学者,变量sqlFound应该在 while 循环中声明,或者至少在 while 循环中重新初始化。 For example例如

   while (runAgain == true) 
   {
       bool solFound = false;
       //...

Otherwise it will keep the value of a previous iteration of the loop.否则它将保留循环的前一次迭代的值。

In this if statement在这个 if 语句中

  if (solFound != true)
  {
     printf("No solution found\n\n");
     runAgain = false;
  }

setting the variable runAgain设置变量runAgain

 runAgain = false;

does not make a sense because after this if statement you are asking again whether to repeat the loop没有意义,因为在这个 if 语句之后你再次询问是否重复循环

  printf("Run program again? Type 'q' to quit or any other key to continue:");
  if (input != 'q')
  {
     runAgain = true;
  }

So remove this setting所以去掉这个设置

 runAgain = false;

from the if statement.来自 if 语句。

Another problem is that this call of scanf另一个问题是 scanf 的这个调用

  scanf("%c", &input);

must follow the question必须跟随问题

  printf("Run program again? Type 'q' to quit or any other key to continue:");

And moreover the format string must contain a leading blank而且格式字符串必须包含一个前导空白

  scanf(" %c", &input);
        ^^^^^

Otherwise white space characters will be read from the input stream. That is you need to write否则将从输入 stream 中读取空白字符。那是你需要写的

  printf("Run program again? Type 'q' to quit or any other key to continue:");

  scanf(" %c", &input);

  runAgain = input != 'q' && input != 'Q';

You're going to a lot of trouble to make sure that runAgain is false before you ask the user to run again.在要求用户再次运行之前,要确保runAgainfalse ,你会遇到很多麻烦。 This isn't doing what you think:这不是你想的那样:

if (solFound != true)
{
   printf("No solution found\n\n");
   runAgain = false;
}

Once solFound becomes true , it stays that way, and if when the program loops there is no solution, you fail to print "No solution found" as well as failing to change runAgain to false.一旦solFound变为true ,它就会保持这种状态,如果程序循环时没有解决方案,您将无法打印“未找到解决方案”以及无法将runAgain更改为 false。

This causes a problem if runAgain is already true :如果runAgain已经是true ,这会导致问题:

  if (input != 'q')
  {
     runAgain = true;
  }

This lets you change false (if there is no solution) back to true (by entering something other than 'q' ).这使您可以将false (如果没有解决方案)改回true (通过输入'q'以外的内容)。 But entering 'q' can't change true to false !但是输入'q'不能将true变为false

The problem only stops if runAgain went false and you enter 'q' .只有当runAgain假并且您输入'q'时问题才会停止。

If you wanted to stop only based on entry of q , you can eliminate the connection in the first block between solFound and runAgain , and replace the second block by:如果您只想根据q的输入停止,您可以消除第一个块中solFoundrunAgain之间的连接,并将第二个块替换为:

runAgain = (input != 'q');

This way runAgain is always changed to match the user choice, never left alone.这样runAgain总是会改变以匹配用户的选择,永远不会单独使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM