Int Array[]打印不好



这是我的代码:

#include <stdio.h>
void main()
   {
   int indeks, a[11], j, rezultat[50];
   int n = 0;
   printf("Unesite elemenate nizan");
   while (n < 10)
   {
     for(indeks = 0; indeks < 10; indeks++);
     scanf("%d", &a[indeks]);
     n++;
   } 
    for (n = 0; n < 10; n++) {
    printf("%dn", a[n]);
   }
}

你好,我有一个问题,这并没有把数组打印成我输入的整数。

它总是打印出-858993460十次。

这就是cmd中的外观。(抱歉英语不好)

 Unesite elemenate niza: 
 1     /input starts here
 3
 5
 1
 0
 2
 3
 5
 7
 4     /ends here
-858993460  
-858993460
-858993460
-858993460
-858993460
-858993460 
-858993460
-858993460
-858993460
-858993460      /output result
Press any key to continue . . .

for循环不执行任何操作,因为它以;结束,并且随着while循环的迭代,indeks将始终为10。我建议使用以下

#include <stdio.h>
int main()                                  // correct function type
    {
    int indeks, a[11], j, rezultat[50];
    int n = 0;
    printf("Unesite elemenate nizan");
    //while (n < 10)                        // delete while loop
    //{
    for(indeks = 0; indeks < 10; indeks++)  // remove trailing ;
        scanf("%d", &a[indeks]);
    //n++;                                  // delete unnecessary line
    //} 
    for (n = 0; n < 10; n++) {
        printf("%dn", a[n]);
    }
   return 0;                                // add return value
}

这个for(indeks = 0; indeks < 10; indeks++);除了将CCD_ 7增加10倍之外什么也不做。我可以为你写完整的代码,但那时你将如何学习?

您的代码似乎有几个语法错误Weather Vane已经发布了正确的版本,请查看他的答案。

#include <iostream>
#include <stdio.h>
void main()
{
    const unsigned int A_SIZE( 10 );
    int a[ A_SIZE ];
    printf( "Unesite elemenate nizan" );
    for ( unsigned int indeks( 0 ); indeks < A_SIZE; ++indeks )
        scanf( "%d", &a[ indeks ] );
    for ( unsigned int indeks( 0 ); indeks < A_SIZE; ++indeks )
        printf( "%dn", a[ indeks ] );
    std::cout << "Enter a character to exit: "; char c; std::cin >> c;
}

最新更新