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