C,如果..else if..else问题 [英] C, if ..else if..else problem

查看:65
本文介绍了C,如果..else if..else问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的if语句有问题..

我被问到的是采用aray的0 + 4 * n和3 + 4n元素但仅针对n-5值数组..对于剩下的5个值取第一个和最后一个..

然后仅在这些值中,检查数组中的值是否为0,如果它为零则使其成为1.

我的代码在下面..它有点改变,因为在我的代码中总数是通过我跳过的txt文件提供的..

问题到底是什么...

else语句是用其他if语句激活的...

有什么建议吗?



我尝试过:



 #include< stdio.h> 
int main( void
{
int array [ 63 ];
int i,n,total;
total = 50 ;

char 选择;

for (i = 0 ; i< total; i ++)
array [i] = 0 ;

执行 {
printf( 0.Exit \ n);
printf( 1.List array \\\
);
printf( 2.更改数组的值:\ n);
scanf( %c,& selection);

if (selection == ' 1'
{
for (i = 0 ; i< total; i ++)
printf( %d \ n,阵列[I]);
}

else if (selection == ' 2'
{
for (i = 0 ; i<(total); i ++)
{
for (n = 0 ; n<((total)/ 4); n ++)
{ if (i ==(0+(n * 4))&& i<(total-5)&&(array [i] == 0 ))
{
array [i] = 1 ;
printf( 您更改了数组值%d,,i);
i = n =总数+ 1;
break ;
}


其他 如果((i ==(3+(n * 4)))&& i<(total-5)&&(array [i] == 0 ) )
{
array [i] = 1 ;
printf( 您更改了数组值%d,,i);
i = n =总数+ 1;
break ;
}
else if (i ==(total-5)& &(array [i] == 0 ))
{
array [i] = 1 ;
fprintf( 你改变了数组值%d,,i);
i = n =总数+ 1;
break ;
}
else if (i ==(total-1)& &(array [i] == 0 ))
{
array [i] = 1 ;
printf( 您更改了数组值%d,,i);
i = n =总数+ 1;
break ;
}
else
printf( 无需更改);

}
}
}
} (选择!= ' 0');
}

解决方案

如果我正确理解你的问题,你说的是else打印语句正在打印时你希望else if被执行。这是不可能的,但你确实在嵌套的for循环中有这个,所以也许它会在下一次迭代时到达else而不是预期的。



我'我不确定你要用这个代码做什么(这很难读,我建议重构),但是你的意思是在那里有破解语句吗?那些会让你脱离for循环。


你的代码不完整,我们无法设计它应该做什么。请完成它,以便我们了解您的尝试。

唯一可能的建议:

您应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近一个bug。



[UpDate]

使用调试器,检查

 i = n =总计+ 1; 



做你认为应该做的事。


Hello i have a problem with if statements..
What i was asked is to take the 0+4*n and 3+4n elements of the aray but only for the n-5 values of the array.. For the remaining 5 values to take the first and the last one ..
Then in these values only, check if the value in the array is 0 and if it is zero make it 1.
My code is below..It is a little bit altered since in my code the total number is provided through a txt file that i skipped here..
The problem is in the end...
else statement is activated with other else if statements ...
Any suggestions?

What I have tried:

#include <stdio.h>
int main(void)
{
int array[63];
int i,n,total;
total=50;

char selection;

for (i=0; i<total; i++)
array[i] = 0;

do{
printf("0.Exit \n");
printf("1.List array \n");
printf("2.Change value of array: \n");
scanf(" %c",&selection);

if (selection=='1')
{   
    for (i=0; i<total; i++)
    printf("%d \n", array[i]);
}

else if (selection=='2')
    {
        for (i=0; i<(total); i++)
        {
            for (n=0; n<((total)/4); n++)
            {   if (i==(0+(n*4)) && i<(total-5)&&(array[i]==0))
                {
                    array[i]=1;
                    printf("You changed array value in place %d, ",i);
                    i=n=total+1;
                    break;
                }


                else if ((i==(3+(n*4)))&&i<(total-5)&&(array[i]==0))
                {
                    array[i]=1;
                    printf("You changed array value in place %d, ",i);
                    i=n=total+1;
                    break;
                }
                else if (i==(total-5)&&(array[i]==0))
                {
                    array[i]=1;
                    fprintf("You changed array value in place %d, ",i);
                    i=n=total+1;
                    break;
                }
                else if (i==(total-1)&&(array[i]==0))
                {
                    array[i]=1;
                    printf("You changed array value in place %d, ",i);
                    i=n=total+1;
                    break;
                }
                else
                    printf("Nothing to change");

            }
        }
    }
  } while (selection!='0');
}

解决方案

If I understand your question correctly, you're saying that "else" print statement is getting printed when you expect an "else if" to be getting executed. That's impossible, but you do have that inside a nested for loop, so perhaps it's getting to the "else" on the following iteration when it's not expected.

I'm not sure what you're trying to do with that code (it's hard to read, I'd suggest refactoring), but do you mean to have the "break" statements in there? Those will break you out of the for loop.


Your code is not complete, we can't devise what it is supposed to do. Please complete it, so that we can understand what you try to do.
The only possible advice:
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[UpDate]
With the debugger, check that

i=n=total+1;


do what you think it should.


这篇关于C,如果..else if..else问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆