我该如何修复此代码?我是新手! [英] How can i fix this code? i'm a newbie!

查看:61
本文介绍了我该如何修复此代码?我是新手!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试学习C编程,我正在做一些简单的项目。

我现在要做的任务是:



  #include   <   stdio.h  >  
< span class =code-keyword> #include < stdlib.h >

int main( void
{
int numb,i,numb2,n ;
i == 0 ;
scanf( %d,& numb);
numb2 =麻木;
while (numb2!= 0 ){
numb2 = numb2 / 10 ;
++ i;
printf( %d \ n,numb2);
}
printf( %d,i);
char array [i];
for (n = i; n = 0 ; n = i - 1 ){
if (numb% 10 == 0 ){
array [n] = ;
}
如果(麻木% 10 == 1 ){
array [n] = < span class =code-string> one;
}
如果(麻木% 10 == 2 ){
array [n] = < span class =code-string> two;
}
如果(麻木% 10 == 3 ){
array [n] = < span class =code-string> three;
}
如果(麻木% 10 == 4 ){
array [n] = < span class =code-string> four;
}
如果(麻木% 10 == 5 ){
array [n] = < span class =code-string> five;
}
如果(麻木% 10 == 6 ){
array [n] = < span class =code-string> six;
}
如果(麻木% 10 == 7 ){
array [n] = < span class =code-string> seven;
}
如果(麻木% 10 == 8 ){
array [n] = < span class =code-string> 8;
}
如果(麻木% 10 == 9 ){
array [n] = < span class =code-string> nine;
}
}
for (n = 0 ; n = i; ++ n){
printf( %c \ n,< span class =code-keyword> array [n]);
}
return 0 ;
}





我的尝试:



我无法弄清楚我的代码有什么问题。我的'i'变量在第一次循环时出现了问题,但无法解决。请帮助。





感谢!!!!

解决方案

首先使用调试器。

在第一行代码上放置一个断点,并运行你的程序:

 i ==  0 ; 



(我无法告诉你如何做到这一点,我不知道你有哪些调试器 - 但谷歌会告诉你,如果你看断点和你的开发环境。)



当它到达那一行时,它会停止,让你控制。

逐步执行您的程序行,使用调试器查看包含的变量,并在执行之前确定下一行代码应该执行的操作。它完全符合您的预期吗?如果是这样,继续前进。如果没有 - 为什么不呢?开始寻找导致这种情况发生的原因。


这是一项有价值的技能 - 可预测的足够调试 - 就像所有技能一样,你只需要通过使用它就可以做得更好它。所以试一试看看你能找到什么。



我们不能为你做到这一点 - 我们不知道那个程序应该做什么,你运行它时输入的更少! :笑:


这段代码有几个问题。

首先,它甚至不会编译。

  char   array  [i]; 



数组声明需要常量大小。

如果你想要一个可变大小的数组那么你必须以不同的方式做事。

为简单起见,现在只需要声明一个常量的数组。

但是,在此之前,数组被声明为包含在每个数组中元素个别字符,而不是字符串。该数组需要包含指向的指针( char * ),这实际上是 strings 。这:

  char  *  array  [ 10 ]; 

将起作用(例如输入数字最多10位)。



接下来,for循环是< b>非常破碎。第一个永远不会执行循环体,第二个永远不会终止,除非输入 numb 为0.

该陈述的第二项是循环终止条件。如果此条件返回逻辑假(即零),则循环终止。你已经在那里放置了赋值而不是比较。

在第一个循环中,你的代码分配给 n 值为0并返回0,所以终止条件为假,循环永远不会输入。

在第二个循环中,您的代码分配给 n <$ c $的值c> i 并返回该值。因此,如果 i 为0,则永远不会输入循环,否则它将永远不会终止。

 的类=code-keyword>(n = i; n> =  0 ; --n){



  for (n =  0 ; n< = i; ++ n){



注意,第一个for循环的第三项的更改。由于 i 在循环体中从未改变过,因此您的代码会在每次迭代时将 n 设置为相同的值,几乎在所有情况下再次造成无限循环。



最后, i == 0; 是一个毫无意义的比较0.您可能想要 i = 0;



这还有很多其他问题代码,它仍然不会做你想要的,但它应该编译和可调试。您需要立即开始学习如何在编译环境中使用调试器。开发它是一项基本技能,它将帮助您学习如何正确编码,当您可以看到您的代码实际做什么时它不做你做什么期待!

Hello I'm trying to learn C programming and I'm exercising doing some easy projects.
The task I'm trying to do now is this:

 #include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int numb , i , numb2 , n ;
    i == 0;
    scanf("%d" , &numb);
    numb2 = numb;
    while ( numb2 != 0 ){
        numb2 = numb2 / 10;
        ++i;
        printf("%d \n" , numb2 );
    }
    printf("%d" , i);
    char array[i];
    for ( n = i; n = 0; n = i - 1 ) {
    if(numb % 10 == 0 ){
        array[n] = "zero";
        }
        if(numb % 10 == 1){
            array[n] = "one";
        }
        if(numb % 10 == 2){
            array[n] = "two";
        }
        if(numb % 10 == 3){
            array[n] = "three";
        }
        if(numb % 10 == 4){
            array[n] = "four";
        }
        if(numb % 10 == 5){
            array[n] = "five";
        }
        if(numb % 10 == 6){
            array[n] = "six";
        }
        if(numb % 10 == 7){
             array[n] = "seven";
        }
        if(numb % 10 == 8){
            array[n] = "eight";
        }
        if(numb % 10 == 9){
            array[n] = "nine";
        }
    }
    for (n=0; n=i; ++n){
        printf("%c \n", array[n]);
    }
    return 0;
}



What I have tried:

I can't figure out what is wrong with my code.There is a problem with my 'i' variable at the first loop but can't fix it.Please help.


Thanksss!!!!

解决方案

Start by using the debugger.
Put a breakpoint on the first line of code, and run your program:

i == 0;


(I can't tell you how to do that, I don't know what debuggers you have available - but Google will tell you if you look up "breakpoint" and your development environment.)

When it reaches that line, it will stop, and let you take control.
Step through your program line by, using the debugger to look at what variables contain, and work out what the next line of code should do before you execute it. Did it do exactly what you expected? If so, move on. If not - why not? Start looking for what causes that to happen.

This is a valuable skill - called predictably enough "debugging" - and like all skills you only get better at it by using it. So give it a try and see what you can find out.

We can't do that for you - we have no idea what that program is supposed to do, much less what you entered when you ran it! :laugh:


There are several things wrong with this code.
First of all, it won't even compile.

char array[i];


The array declaration requires a constant size.
If you want a variable sized array then you have to do things differently.
For simplicity, just declare the array with a constant size for now.
But, before that, the array is declared as containing in each element individual characters, not strings. The array will need to contain pointers to characters (char*) which is what strings are actually, under the covers. This:

char* array[10];

will work (for the input number up to 10 digits, for example).

Next, the for loops are very broken. The first one will never execute the body of the loop at all, and the second one will never terminate unless the numb was entered as 0.
The second term of the statement is the loop termination condition. If this condition returns logical false (that is, zero) then the loop terminates. You have placed assignment, not comparison, expressions there.
In the first loop your code assigns to n the value 0 and returns 0, so the termination condition is false and the loop is never "entered".
In the second loop you code assigns to n the value of i and returns that value. So if i is 0, the loop will never be entered, otherwise it will never terminate.

for (n = i; n >= 0; --n) {


for (n = 0; n <= i; ++n){


Note, the change to the third term of the first for loop. Since i never changed during the body of the loop, your code would have set n to the same value at each iteration, again causing an infinite loop in almost all cases.

Finally, the i == 0; is a pointless comparison to 0. You probably wanted i = 0;

There are plenty of other things wrong with this code, and it still won't do what you intend but it should compile and be debuggable. You need to start right away at learning how to use the debugger for your compilation environment. It is an essential skill to develop and it will help you to learn how to code correctly when you can see what your code is actually doing when it isn't doing what you expect!


这篇关于我该如何修复此代码?我是新手!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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