Int Array[ ] 打印效果不佳。
Int Array[ ] not printing good.
这是我的代码:
#include <stdio.h>
void main()
{
int indeks, a[11], j, rezultat[50];
int n = 0;
printf("Unesite elemenate niza\n");
while (n < 10)
{
for(indeks = 0; indeks < 10; indeks++);
scanf("%d", &a[indeks]);
n++;
}
for (n = 0; n < 10; n++) {
printf("%d\n", 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 niza\n");
//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("%d\n", a[n]);
}
return 0; // add return value
}
这个
for(indeks = 0; indeks < 10; indeks++);
除了递增 indeks
10 次外什么都不做。
我可以为您编写更正的整个代码,但您将如何学习?
您的代码似乎有几个语法错误。 Weather Vane已发布正确版本,请看他的回答。
#include <iostream>
#include <stdio.h>
void main()
{
const unsigned int A_SIZE( 10 );
int a[ A_SIZE ];
printf( "Unesite elemenate niza\n" );
for ( unsigned int indeks( 0 ); indeks < A_SIZE; ++indeks )
scanf( "%d", &a[ indeks ] );
for ( unsigned int indeks( 0 ); indeks < A_SIZE; ++indeks )
printf( "%d\n", a[ indeks ] );
std::cout << "Enter a character to exit: "; char c; std::cin >> c;
}
这是我的代码:
#include <stdio.h>
void main()
{
int indeks, a[11], j, rezultat[50];
int n = 0;
printf("Unesite elemenate niza\n");
while (n < 10)
{
for(indeks = 0; indeks < 10; indeks++);
scanf("%d", &a[indeks]);
n++;
}
for (n = 0; n < 10; n++) {
printf("%d\n", 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 niza\n");
//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("%d\n", a[n]);
}
return 0; // add return value
}
这个
for(indeks = 0; indeks < 10; indeks++);
除了递增 indeks
10 次外什么都不做。
我可以为您编写更正的整个代码,但您将如何学习?
您的代码似乎有几个语法错误。 Weather Vane已发布正确版本,请看他的回答。
#include <iostream>
#include <stdio.h>
void main()
{
const unsigned int A_SIZE( 10 );
int a[ A_SIZE ];
printf( "Unesite elemenate niza\n" );
for ( unsigned int indeks( 0 ); indeks < A_SIZE; ++indeks )
scanf( "%d", &a[ indeks ] );
for ( unsigned int indeks( 0 ); indeks < A_SIZE; ++indeks )
printf( "%d\n", a[ indeks ] );
std::cout << "Enter a character to exit: "; char c; std::cin >> c;
}