【问题标题】:Program to tell if a number is even or odd. its sayng theres no previous 'if' before 'else' [duplicate]程序来判断一个数字是偶数还是奇数。它说在'else'之前没有以前的'if' [重复]
【发布时间】:2019-03-25 23:19:14
【问题描述】:

我打算制作一个程序来告诉用户给定的整数是偶数还是奇数。但是,它说'else'没有以前的'if',我错了什么?

#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;

printf("type an integer ");
scanf("%d", &num);

if(num % 2 == 0);
    printf("%d is even", num);

else
    printf("%d is odd", num);

return 0;
}

没有输出

【问题讨论】:

  • if 行的末尾删除;。并习惯于包裹 every 然后用大括号阻塞,它会帮助你很多

标签: c


【解决方案1】:

您的代码

if(num % 2 == 0);
   printf("%d is even", num);

修复

if(num % 2 == 0)
   printf("%d is even", num);

问题

你的 if 语句后面有一个分号,删除它会解决你的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多