c - 简单数组索引失败

标签 c arrays

为什么这不起作用......?

意图:想要拥有它,以便用户选择一个表格进行练习 然后对所选择的数字和数字数组进行求和。 我假设它会从数组中随机选择一个数字?

#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/

相关文章:

c - GCC 生成文件不接受 -std=c99 -lm

c - 将四个嵌套循环转换为 CUDA 内核

c - 了解 C 中的内存访问

javascript - 在 JavaScript 中获取嵌套 JSON 数组的长度

java - java中 double 的选择排序

javascript - 如何在 ReactJS 中为 API 中的每个渲染元素显示函数返回的不同值?

java - C代码需要调用Java代码缓冲区但无法获取其JNIEnv

C - 打印控制台可删除的字符串

javascript - 将长 Jade 数组变量拆分为多行

java - 验证二维数组是否有两个相等行的算法