【问题标题】:Prints only "An error occurred in the program!"仅打印“程序中发生错误!”
【发布时间】:2016-11-09 00:37:56
【问题描述】:
#include <stdio.h>

int main(){

    int age;
    char gender;
    printf("Are you male or female (m/f)?");
    scanf("%c",&gender);
    printf("Enter your age:");
    scanf("%d",&age);
    printf("\n");

    if(gender=="m"){
        if(age>=0 && age<=55){
         printf("You are a man in your best years!\n");
        }
        else if (age>=56 && age<=100){
            printf("You are a wise man!\n");}
        else{
    printf("An error occurred in the program!");}
    }
    else if(gender=="f"){
    if(age>=0 && age<=55){
         printf("You are a beauty!\n");
        }
        else if (age>=56 && age<=100){
            printf("You look young for your age!\n");}
        else{
    printf("An error occurred in the program!");}

    }
    else{
    printf("An error occurred in the program!");}
    return 0;
}

程序运行不正常。它只打印最后的 else 语句打印,即“程序中发生错误!”。我还尝试将所有条件放在同一个小括号中

喜欢:

if(gender=="f"&& age>=0 && age<=55)) 

但它仍然没有输出预期的结果。

【问题讨论】:

  • gender=="m" --> gender=='m'
  • 以此类推,同样适用于else if(gender=="f")
  • 谢谢它的工作。

标签: c


【解决方案1】:

您将charstring 文字(gender == "m") 进行比较。双引号用于字符串。在 m (gender == 'm') 和 f (gender == 'f') 周围使用单引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    相关资源
    最近更新 更多