c - 范围内的随机发生器

标签 c visual-studio-2010 random getch

嗨,我知道范围内的随机生成器已经存在一个问题,但我不明白。我是C初学者,只懂java。在这个程序中,我试图用 C 创建一个数学导师。该程序将随机生成从 1 到 10 的两个数字和一个运算符。它运行,但不显示下一行,并且一直显示错误的答案。另外,为什么VS2010说getch()未定义?这是代码:

 int ans;
 int ans1;
 int num1 = rand() % 10 + 2;
 int num2 = rand() % 10;
 int operation = rand() % 4;

    printf("\tMATH TUTOR\n");
    if(operation == 1){
        printf("What is %d + %d ?", num1, operation, num2);
        scanf_s("%d",ans1);
        ans = num1 + num2;
        if(ans != ans1){
            printf("Incorrect! Try Again!");
            do{
                scanf_s("%d", &ans1);
            }while( ans != ans);
        }else{
            printf("Correct!");
        }
        }else if(operation == 2){
            printf("What is %d - %d ?", num1, operation, num2);
            scanf_s("%d",&ans1);
            ans = num1 - num2;
            if(ans != ans1){
                printf("Incorrect! Try Again!");
                do{
                    scanf_s("%d", &ans1);
                }while( ans != ans);
            }else{
                printf("Correct!");
                }
        }else if(operation == 3 ){
            printf("What is %d * %d ?", num1, operation, num2);
            scanf_s("%d",&ans1);
            ans = num1 * num2;
            if(ans != ans1){
                printf("Incorrect! Try Again!");
                do{
                    scanf_s("%d", &ans1);
                }while( ans != ans);
            }else{
                printf("Correct!");
            }
            }else if(operation == 4){
                printf("What is %d / %d ?", num1, operation, num2);
                scanf_s("%d",&ans1);
                ans = num1 / num2;
                if(ans != ans1){
                    printf("Incorrect! Try Again!");
                    do{
                        scanf_s("%d", &ans1);
                    }while( ans != ans);
                }else{
                    printf("Correct!");
                }
            }

    getch();
    return 0;
}

最佳答案

添加到约翰·谢里登的:getch()是许多 MS-DOS 编译器添加的 C 的非标准扩展。它通常在 <conio.h> 中定义。 。不知道VS2010默认是否支持。

关于c - 范围内的随机发生器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17391589/

相关文章:

c++ - 通过多个 fork 管道标准输入/输出

我可以在 if 语句中将字符用作整数吗?

比较C中的数字

c++ - 在应用程序中包含正确的 C++ redist

javascript - 每秒生成 0 到 100 之间的增量随机数,其中新数字应大于前一个数字

C:声明一个指针然后将其用作数组,内存分配

visual-studio-2010 - MSBuild调用Web项目的Package Target

c# - 生成没有忽略值的随机字节数组

php - mt_rand + time() 生成的 key 如何准确且唯一以用作预订引用 ID?

c# - 将图片框中的图像更改为棕褐色