我现在正在学习C,我想写一个程序,它可以用一个数字ganzeZahl来确定数组的长度。
然后,您必须输入存储在大小为n的数组中的数字,然后它应该执行选择排序(我在这里将其去掉,因为我的程序甚至没有到达这一部分)。
每当我尝试运行while循环时,我都无法通过它。它编译得很好。它永远不会到达printf("!!!--------!!!"); //由于某种原因无法到达此部分?test5。
#include<stdio.h>
int main() {
int ganzeZahl;
scanf("%d", &ganzeZahl);
//printf("ganze Zahl ist: %d\n", ganzeZahl); //test
int array[ganzeZahl];
int i = 0;
for(int z = 0; z < ganzeZahl; z++) {
printf("%d\n", array[z]);
}
while(i<ganzeZahl) {
//printf("!!!hier!!!\n"); //test2
scanf("%d", &array[i]);
//printf("zahl gescannt!\n"); //test 3
i++;
//printf("i erhöht!\n"); //test 4
}
printf("!!!--------!!!"); //this part isn't reached for some reason? test5
//selection sort here
//[...]
return 0;
}发布于 2021-01-03 20:03:48
您的程序确实可以正确执行,并最终到达最后一个printf调用。
当它进入while循环时,它会继续调用scanf,这会导致它停止并等待,直到您在每次迭代中输入值。如果您提供ganzeZahl输入(输入一个数字并按下'enter'),它将完成循环并继续。我猜如果您在循环内的scanf之前添加一个printf,它应该会更直观。
发布于 2021-01-03 20:20:43
for(int z = 0; z < ganzeZahl; z++){
printf("%d\n", array[z]);该数组尚未初始化,因此您还不能打印该数组。
实际上,您搞乱了code.The while循环应该先出现,然后是for循环的顺序。我已经更正了你下面的代码。祝你编码愉快!
#include<stdio.h>
int main() {
int ganzeZahl;
scanf("%d", &ganzeZahl);
//printf("ganze Zahl ist: %d\n", ganzeZahl); //test
int array[ganzeZahl];
int i = 0;
while(i<ganzeZahl) {
//printf("!!!hier!!!\n"); //test2
scanf("%d", &array[i]);
//printf("zahl gescannt!\n"); //test 3
i++;
//printf("i erhöht!\n"); //test 4
}
for(int z = 0; z < ganzeZahl; z++) {
printf("%d\n", array[z]);
}
printf("!!!--------!!!"); //this part isn't reached for some reason? test5
//selection sort here
//[...]
return 0;
}https://stackoverflow.com/questions/65549481
复制相似问题