为什么这不起作用......?
意图:想要拥有它,以便用户选择一个表格进行练习 然后对所选择的数字和数字数组进行求和。 我假设它会从数组中随机选择一个数字?
#include <stdio.h>
int main(){
int Sum [12] = {1,2,3,4,5,6,7,8,9,10,11,12};
int Choice ;
printf ("Pick Your Time's Table\n");
printf ("|2|3|4|5|6|7|8|9|10|11|12|\n");
printf ("===========================\n");
scanf ("%d",Choice);
printf ("%d x %d",Choice,Sum);
return 0;
}
最佳答案
好吧,你在这里遇到了一些问题。实际上,这不是你需要数组的问题。您需要的是生成从 1 到 12 的随机数。这是我根据您的示例程序编写的示例程序,您可以修改它来执行您想要的操作。
#include <stdio.h>
#include <cstdlib>
int main()
{
//declare variables we will use later
int choice = 1;
int randomNumber;
//print prompt for user
printf("Pick Your Times Table\n");
printf("|2|3|4|5|6|7|8|9|10|11|12|\n");
printf("===========================\n");
printf("Enter 0 to quit.\n");
while (choice != 0)
{
//read in user input
scanf_s("%d", &choice);
//gets a random number from 1 and 12
randomNumber = rand() % 12 + 1;
printf("%d x %d = ?\n", choice, randomNumber);
system("pause"); //pauses so the answer isnt shown right away.
//print the result of the multiplication problem
printf("= %d\n", choice * randomNumber);
//get user input again
printf("Pick Your Times Table\n");
printf("|2|3|4|5|6|7|8|9|10|11|12|\n");
printf("===========================\n");
printf("Enter 0 to quit.\n");
}
return 0;
}
一开始从无到有地编写代码是非常困难的,所以我希望以此作为修改示例对您有所帮助。如果您还有任何代码中的注释无法解答的问题,请告诉我。
关于c - 简单数组索引失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30879912/