c - C 语言中 else 没有前面的 if

标签 c if-statement

我知道 else 语句不应在 if 语句末尾有分号。

目标是使这个 if/else 语句起作用,以便如果答案不是 Y 或 y,则打印 else 语句。我的问题是:

  • 到底需要做什么来纠正这个问题?

代码如下:

if (answer=='Y'|| 'y')
scanf(" c", &answer);
printf("\n Great! Keep listening to music. I'm sure your mood will improve.");
else printf("\n Try listening to some music that you enjoy!");
}

最佳答案

The objective is to make this if/else statement work,

您的代码中缺少花括号。

scanf(" %c", &answer);
 if (answer=='Y'|| answer == 'y') {
     printf("\nGreat! Keep listening to music. I'm sure your mood will improve.\n");
 }
 else {
     printf("\nTry listening to some music that you enjoy!\n");
 }

关于c - C 语言中 else 没有前面的 if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42048183/

相关文章:

使用c中的结构进行复杂 vector 加法

c++ - volatile 变量存储在哪里?

比较 if 语句中的指针值

C 编译器预处理器输出

c - 用逗号分隔的多个值的宏实际上是什么意思?

c - 向结构体添加值,在 C 中添加会出错

javascript - 如果 var not set 总是结果为真,即使它的设置

java - 生命游戏 算法/if else 语句的结构

c++ - C++ 中的 IF 循环行为

javascript - 如果数字在数字部分中