尽管使用了完全相同的输入,但输出却不一致

问题描述 投票:1回答:2

我是C语言的新手,并且通常是编程人员,因此,如果我的问题很容易解决,我谨向您道歉。我搜索了类似的问题,但没有一个回答我的具体问题。

我正在做一个练习,要求编写一个程序,该程序打印两个用户输入的整数之间的所有偶数。我通过确保如果第一个输入小于第二个输入,则程序会递增计数(2、4、6等),如果第一个输入递减计数(6、4、2等),则对练习进行了扩展。输入大于秒。我还为用户提供了继续尝试的机会,但是尽管输入了完全相同的输入(例如2和22),该程序仍将随机打印正确的输出,或不打印任何内容。

我在输出中包含了代码,以帮助说明。谢谢...

`#include <stdio.h>

int i, num1, num2;
char agn;

int main(void)
{
        printf("Display all even numbers falling between two integers.\n\n");
    do {
        printf("Enter your first number: ");
        scanf(" %d", &num1);
        printf("\nEnter your second number: ");
        scanf(" %d", &num2);

        printf("\n");

        if (num1 % 2 != 0)
        {
            num1++;
            i = num1;
        }
            // The following if statement decrements if the first number   entered is greater than the second.
            if (i >= num2)
            {
                while (i >= num2 + 2) // Adding 2 to num2 prevents i decrementing to the even number below num2
                    // eg. the printf function below decrements x2, so if the second input is 3, when i = 3 its actual value decrements to 1, therefore making 2 become the last number plotted. This is wrong because 2 is less than the input value of 3. The last number plotted should be 4.
                    printf("%d, ", i -= 2);
            }
            // The else if statement incements if the second number entered is higher than the first, so we must reduce the incremented value of num1 by -2.
            else if (num1 <= num2)
            {
                i = num1 - 2; // num1 was initially incremented by 1. This now decrements num1 by -2
                    while (i <= num2 - 2)
                    printf("%d, ", i += 2);
            }
                    printf("\n\nWould you like to try again? (Y/N): ");
                    scanf(" %c", &agn); // Remember to put a space before %c

                if (agn == 'n')
                        {
                            agn = 'N';
                        }
        }
    while (agn != 'N');

            printf("\n\n");
    return 0;
}`

输出:显示所有介于两个整数之间的偶数。

输入您的第一个电话号码:2

输入您的第二个号码:22

2,4,6,8,10,12,14,16,18,20,22,

您想再试一次吗? (是/否):y输入您的第一个电话号码:22

输入您的第二个号码:2

20,18,16,16,14,12,10,8,6,4,2,

您想再试一次吗? (是/否):y

输入您的第一个电话号码:22

输入您的第二个号码:2

(---无输出---)

您想再试一次吗? (是/否):y输入您的第一个电话号码:2

输入您的第二个号码:22

2,4,6,8,10,12,14,16,18,20,22,

您想再试一次吗? (是/否):n

程序以退出代码结束:0

c
2个回答
2
投票

您并不总是初始化i。如果第一个数字为奇数,则初始化i = num1,然后在递增分支中初始化i = num1 - 2,但在递减分支中不进行初始化。由于您先运行了递增分支,因此i保留如下:

i = num1 - 2; // i = 0
while (i <= num2 - 2) // i <= 20
  printf("%d, ", i += 2); // i is left as 22

因此,当您随后运行递减循环时,偶然完全正确设置了i

if (i >= num2) // true, i = 22 from before, num2 = 2
{
    while (i >= num2 + 2) // i >= 4
        printf("%d, ", i -= 2); // i = 2
}

但是当您尝试再次运行递减循环时,i为2,而不是22。所以您得到了

if (i >= num2) // true, i = 2, num2 = 2
{
    while (i >= num2 + 2) // i >= 4, immediately false
        printf("%d, ", i -= 2); // never runs
}

我建议将您的代码重新构造为两个函数。 main()应该处理用户输入和程序的状态(num1,num2),然后将其传递给具有局部变量printSequence的辅助函数printForward(或两个,printReversei) 。这将有助于解决此类错误,因为所有的长期状态都将限于main()


0
投票

似乎我在处理这个问题方面已经超越了我的能力,只是学习C的几周时间。将值传递给辅助函数是我在改变结构之前必须尽快掌握的知识。该程序的。不过,作为一种解决方法,我采用了Mathias提供的解决方案,该解决方案是简单地删除i = num1;从if语句花括号。尽管我仍然必须解决几个问题,但这是可行的。这就是我对这款游戏的热爱-它带来的挑战。谢谢大家。非常感谢您的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.