0

我的 C 项目是一个 Windows 控制台应用程序,它从用户的音乐项目中获取拍号和 BPM,并以秒为单位返回小节的长度。
我正在尝试使用 do-while 循环来添加“继续/退出?” 在每次成功计算结束时提示这里是该函数的源代码。它根据用户输入执行一项计算,然后终止。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char timeSignature[6];
    float BPM;
    float beatsPerBar;
    float barLength;

    printf("Enter the working time signature of your project:");
    scanf("%s",timeSignature);

    beatsPerBar = timeSignature[0]-'0';

    printf("Enter the beats per minute:");
    scanf("%f", &BPM);

    barLength = BPM / beatsPerBar;

    printf("%f\n", barLength);
    return 0;
}

每次计算成功后,我想提示用户选择“y”返回初始输入提示或“n”结束程序并退出命令提示。稍后的更新包括一个 do-while 循环,旨在添加该功能。

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <unistd.h>

int main()
{

    do{

        char timeSignature[6];
        char anotherCalculation;
        float BPM;
        float beatsPerBar;
        float barLength;

        printf("Enter the working time signature of your project:");
        scanf("%s",timeSignature);

        beatsPerBar = timeSignature[0]-'0';
        /*
         * Subtracts the integer value of the '0' character (48) from the integer value
         * of the character represented by the char variable timeSignature[0] to return
         * an integer value equal to the number character itself.
         */

        printf("Enter the beats per minute:");
        scanf("%f", &BPM);

        barLength = BPM / beatsPerBar;

        printf("%f\n", barLength);

        Sleep(3);

        printf("Would you like to do another calculation? (Y/N)");
        scanf(" %c", &anotherCalculation);

    }while((anotherCalculation = 'Y'||'y'));

    if((anotherCalculation != 'Y'||'y'))
        {
        printf("Goodbye!");
        return 0;
        }

    return 0;
}

当我编译时,没有错误,但是当我运行它时,程序在任何输入后循环。为什么代码忽略了我的真实分配?我能做些什么来解决这个问题?

4

7 回答 7

4

改变这个:

while (anotherCalculation = 'Y'||'y')

对此:

while(anotherCalculation == 'Y'|| anotherCalculation == 'y')

否则你正在做:

while (anotherCalculation = ('Y'||'y'))

请注意,我还将分配更改=为 comparison ==

if以后的情况也是如此:

if (anotherCalculation != 'Y'||'y')

应该:

if (anotherCalculation != 'Y'|| anotherCalculation != 'y')

顺便说一句,这if是多余的,你已经离开了while循环,所以用户不需要更多的计算。

于 2015-09-08T20:49:03.257 回答
3

在进行比较之前将您的值转换为大写字母:

while(toupper(anotherCalculation) == 'Y');

这将接受小写或大写'Y',并保持您的代码简单明了。

于 2015-09-08T20:52:30.703 回答
2

您的问题在于以下代码行:

while((anotherCalculation = 'Y'||'y'));

您的第一个问题是您正在分配变量anotherCalculation然后测试该值。这是因为在 C 中,=(赋值)运算符可以在测试语句中使用,并且设置的值将返回到语句中,基本上使您的测试语句如下:

while('Y'||'y');

=即使在替换for之后==,您的测试语句中的逻辑也存在错误,
您实际上在做什么与以下内容相同:

TRUE如果anotherCalculation == 'Y'或如果'y'
由于'y'实际上有一个值并且FALSE0标准 C 中,因此您的循环将持续运行,因为它总是有一个TRUE值。

要修复所有错误,该行实际需要的是:

while(anotherCalculation == 'Y'|| anotherCalculation == 'y');

最后,如果您想让这段代码更具可读性并避免重复测试,您可以将输入转换为大写并测试:

while(toupper(anotherCalculation) == 'Y');
于 2015-09-08T20:50:14.373 回答
2

首先,您使用数学符号进行逻辑检查,这在 C 中没有意义,并且您使用的是赋值=运算符而不是“等价测试”==运算符。

}while((anotherCalculation = 'Y'||'y'));

应该:

}while((anotherCalculation == 'Y'|| anotherCalculation == 'y'));

其次,为自己省去一些麻烦,并使用它fgets()来获取一行文本并解析它

scanf()最后,用and读入诸如“尾随换行符”之类的东西fgets()例如,该scanf()函数不会“吃掉”你输入末尾的换行符,你会得到意想不到的行为。这个问题至少每月出现一次。

于 2015-09-08T20:50:19.687 回答
2

逻辑运算符不是这样工作的。x == a || b不评估为“x等于ab”之一;相反,它评估为“x等于布尔表达式 ( aOR b) 的结果”。如果要x与不同的值进行比较,则必须将其写为

x == a || x == b 

但是您还有另一个问题,您在=打算使用比较运算符的地方使用了赋值运算符==1

while((anotherCalculation = 'Y'||'y'))
                          ^
                          |
                        ooops

您正在执行两个非零值的逻辑或运算,其计算结果为 1(真),并将结果分配anotherCalculation. 表达式的结果是anotherCalculation赋值 (1) 之后的值,所以你已经有效地写了

while( true )

您需要使用==运算符进行相等比较,并且必须在||运算符的每一侧进行显式比较:

while ( anotherCalculation == 'Y' || anotherCalculation == 'y' )

在进行比较之前,您可以通过将输入转换为特定情况来稍微清理一下,从而完全摆脱对||运算符的需求:

while ( tolower( anotherCalculation ) == 'y' )

或者

while ( toupper( anotherCalculation) == 'Y' )


1.一位名叫 Lee 的聪明的年轻程序员
希望在i3岁时循环,
在写=
他的时候忘记了它的续集
,因此无限循环

于 2015-09-08T21:13:46.173 回答
1

这不是你所期望的:

}while((anotherCalculation = 'Y'||'y'));

这也不是:

if((anotherCalculation != 'Y'||'y'))

你应该这样做:

}while((anotherCalculation == 'Y') || (anotherCalculation =='y'));

和这个:

if((anotherCalculation != 'Y') && (anotherCalculation !='y'))
于 2015-09-08T20:50:25.543 回答
0

TLDR;将您的条件更改为

while( anotherCalculation == 'Y' || anotherCalculation == 'y' );
于 2015-09-08T20:50:38.213 回答