C程序,陷入for循环

时间:2016-06-15 18:15:32

标签: for-loop

我是C的新手。我处在一个无限循环中,无法找出原因。这就是我的尝试。

#include <stdio.h>

int main()
{

    int SEED, TIMES_ROLL,COUNT,DICE1, DICE2;

    //Ask user for seed value
    printf("Type in a number for the seed value?\n");
    scanf("%d", &SEED);

    srand(SEED);

    //Ask user how many times to roll the 2 dice
    printf("How many times would you like to roll the dice?\n");
    scanf("%d", &TIMES_ROLL);

    for (COUNT = 1; COUNT > TIMES_ROLL; COUNT + 1)
    {
            DICE1 = rand() % 6 + 1;
            DICE2 = rand() % 6 + 1;
            printf("%d and %d rolled\n", DICE1,DICE2);
    }

2 个答案:

答案 0 :(得分:0)

你没有增加COUNT,你的标志是错误的:

 for (COUNT = 1; COUNT > TIMES_ROLL; COUNT + 1)

应该是

 for (COUNT = 1; COUNT <= TIMES_ROLL; ++COUNT)

答案 1 :(得分:-1)

for (COUNT = 1; COUNT > TIMES_ROLL; COUNT + 1)中的

,最后一位变为值COUNT+1

您希望将新值分配到COUNT本身。

使用

COUNT = COUNT+1

或更常见的

++COUNT

你也有比较倒置。使用小于或等于COUNT <= TIMES_ROLL作为for语句中的中间表达式

在通常的使用中,计数通常基于零。所以更传统的方式

for (COUNT = 0; COUNT < TIMES_ROLL; ++COUNT)