gpt4 book ai didi

c - 多个 Else If 需要 C 语句吗?

转载 作者:行者123 更新时间:2023-11-30 21:36:40 24 4
gpt4 key购买 nike

所以我询问用户的出生日期,然后根据他们的生肖向他们输出一个简短的句子。我似乎找不到任何有关如何正确处理此问题的信息,或者在这种情况下我应该如何继续处理 switch 语句。非常感谢任何帮助!

int dateOfBirth;

// initiate zodiac animals year of birth
int rat = 2008 || 1996 || 1984 || 1972 || 1960;
int ox = 2009 || 1997 || 1985 || 1973 || 1961;
int tiger = 2010 || 1998 || 1986 || 1974 || 1962;
int rabbit = 2011 || 1999 || 1987 || 1975 || 1963;
int dragon = 2012 || 2000 || 1988 || 1976 || 1964;
int snake = 2013 || 2001 || 1989 || 1977 || 1965;
int horse = 2014 || 2002 || 1990 || 1978 || 1966;
int goat = 2015 || 2003 || 1991 || 1979 || 1967;
int monkey = 2016 || 2004 || 1992 || 1980 || 1968;
int rooster = 2017 || 2005 || 1993 || 1981 || 1969;
int dog = 2019 || 2007 || 1995 || 1983 || 1971;
int pig = 2019 || 2007 || 1995 || 1983 || 1971;


// ask user for date of birth
printf("Please enter your date of birth: \n");
scanf_s("%d", &dateOfBirth);


// FORTUNE IF STATEMENTS
// im tired of these motherfluffing IF STATEMENTS ON MY MOTHERFLUFFING SCREEN!

// rat
if (dateOfBirth == 2008 || 1997 || 1984 || 1972 || 1960)
{
printf("Jerry?%d\n", rat);
}
//ox
elseif(dateOfBirth == 2009 || 1997 || 1985 || 1973 || 1961);
{
printf("Nice horns m8");
}
// tiger
elseif(dateOfBirth == 2010 || 1998 || 1986 || 1974 || 1962);
{
printf("RAAWR\n");
}
// rabbit
elseif(dateOfBirth == 2011 || 1999 || 1987 || 1975 || 1963);
{
printf("HOP HOP HOP %d", rabbit);
}
// dragon
elseif(dateOfBirth == 2012, 2000, 1988, 1976, 1964);
{
printf("RYUJIN NO KEN WO KURAE!");
}
// snake
elseif(dateOfBirth == 2013, 2001, 1989, 1977, 1965);
{
printf("Orochimaru?");
}
// horse
elseif(dateOfBirth == 2014, 2002, 1990, 1978, 1966);
{
printf("NOW WATCH ME NE- No.");
}
// goat
elseif (dateOfBirth == 2015, 2003, 1991, 1979, 1967);
{
printf("Cheese.");
}
// monkey
elseif (dateOfBirth == 2016, 2004, 1992, 1980, 1968);
{
printf("You sure you're not genetically engineered...?");
}
// rooster
elseif (dateOfBirth == 2017, 2005, 1993, 1981, 1969);
{
printf("Who needs an alarm clock amarite?");
}
// dog
elseif (dateOfBirth == 2018, 2006, 1994, 1982, 1970);
{
printf("Haha, funny doggo!");
}
// pig
else (dateOfBirth == 2019, 2007, 1995, 1983, 1971);
{
printf("I don't even like bacon.");
}

system("pause");

}

我希望我包含的代码在格式方面是正确的。

编辑:elseif -> else if。参数末尾也没有分号,以及每年添加“dateOfBirth ==”,代码现在工作正常,感谢所有输入,特别是来自莫斯科的@Vlad!

最佳答案

对于初学者来说,C++ 中没有 elseif 语句。C++ 中有 ifif-else 语句。

此外,您还必须使用 if 语句删除 if 或 else 末尾的错误分号。

而且条件也写错了。

有效的 else 语句可以如下所示

else if ( dateOfBirth == 2009 || dateOfBirth == 1997 || dateOfBirth == 1985 || dateOfBirth == 1973 || dateOfBirth == 1961 )
{
//...
}

关于c - 多个 Else If 需要 C 语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46874242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com