读书人

有关if elseif else的有关问题

发布时间: 2013-04-20 19:43:01 作者: rapoo

有关if elseif else的问题
请问if elseif的结构不是
if
elseif
elseif
....
else
么??
但是我写的程序
if(d==31)
{
if(u==1)
{
y=y+1;
m=1;
d=2;
}
else
{
m=m+1;
d=2;
}
}

else if(d==30)
{
if(u==1)
{
y=y+1;
m=1;
d=1;
}
else
{
m=m+1;
d=1;
}
else if(d==29)
{
if(u==2)
{
m=m+1;
d=1;
}
else if(u==0)
{
m=m+1;
d=2;
}
else
{
d=d=2;
}
else if(d==28)
{
if(u==0||w==0)
{
m=m+1;
d=2;
}
else if(u==0||w==1)
{
m=m+1;
d=1;
}
else
{
d=d+2;
}
else if(d==27)
{
if(u==0||w==0)
{
m=m+1;
d=1;
}
else
{
d=d+2;
}
else
{
d=d+2;
}


咋么会显示illegal else without matching if呢???他说不匹配是怎么回事啊??d=31和30没问题
d=29,28,27和最后一个else都有问题啊??求帮助
[解决办法]
太长了,又没排版,我就不看了。如果你要嵌套的话,应该这样:

if (...) {
...
} else if (...) {
...
} else if (...) {
...
} else if (...) {
...
} else {
...
}

注意,else 永远匹配最近的 if。
[解决办法]
if (...) {
...
} else if (...) {
if (...) {
} else {
}
} else if (...) {
...
} else {
...
}

读书人网 >C++

热点推荐