我有个问题。当我尝试要求用户输入 yes 或 no 作为单个字符并使用括号设置 char 变量时,因为我得到 Y 或 y 在我的 if 语句中无效。如果我不带括号这样做,我只会得到将在 if 语句中评估的 ascii 值。以下代码将无法评估 recAnswer 值。当我在 printf 中输出值时,它会根据输入显示 recAnswer = "Y" 或 "y"。我试过 if(&recAnswer == "Y") 和 if(*recAnswer = "Y")
int main(int argc, char** argv)
{
int numEntered;
char recAnswer[1];
while(numEntered < 1 || numEntered > 15) // Continually ask user to enter a number until the number entered is between 1 and 15 .
{
printf("\nPlease enter a number between 1 and 15:");
scanf(" %d", &numEntered);
}
printf("\nDo you want to get the factorial value recursively? Enter Y or N:"); //Ask user if they want to get the answer recursively
scanf(" %c",&recAnswer);
if(recAnswer == "y" || recAnswer == "Y")
{
printf("The recursive value of %d is %d", numEntered, recursive(numEntered)); //Print out recursive value
}
else
{
printf("The non-recursive value of %d is %d", numEntered, nonRecursive(numEntered)); //Print out looped value
}
return 0;
}
感谢您对此进行调查