【问题标题】:Looping a Function not behaving As I expect. C循环一个函数不符合我的预期。 C
【发布时间】:2014-10-04 23:08:58
【问题描述】:

我正在用 c 语言制作一个简单的游戏,但在从函数 t 传递和接收信息时遇到问题。

我希望代码基本上运行 t 函数,然后打印结果分数。然后,除非其中一个分数大于 3 并且比另一个分数高 2,否则我希望它再次运行 t 并打印结果。

现在它正在运行一次 t 函数然后卡住了。

任何帮助或想法将不胜感激,或者如果我遗漏了信息,请发表评论。

#include <stdio.h>
#include <stdlib.h>

void t(int *score1,int *score2)
{
    int r,p;
    r=rand() % 140;
    if(r>30)
    {
        *score1=score1+1;
    }
    if(30>r)
    {
        *score2=score2+1;
    }
    if(r==30)
    {
    *score2=score2+1;
    }
}

void main(int argc, char *argv[])
{
    srand(time(NULL));
    int score1 =0;
    int score2 =0;
    int x =0;
    while(x==0)
    {
        t(&score1, &score2);
        printf("%d\n",score1);
        printf("%d\n",score2);

        if(score1 > 3 && score1==score2+2)
        {
            printf("Player 1 Wins\n");
            x++;
        }
        if(score2 > 3 && (score2==score1+2))
        {
            printf("Player 2 Wins\n");
            x++;
        }
    }
    return;
}

【问题讨论】:

  • atoi() 解析 const char *,而不是 int *。它旨在解析 ascii 代码数字。你也不需要多次运行srand(time(NULL))。将其移至 while 循环的上一行。
  • 我改变了你说的。我只是没有得到 score1 和 score2 的任何好的值。他们创造了奇怪的价值观。我做错了什么?
  • 如果你学会了正确缩进和格式化你的代码,它会让循环问题更容易追踪。你可以在这里试试。
  • void t(){...} 中的score1score2 是一个指针,它的值通常是一个内存地址。所以你必须把它改成*score1=*score1+1或者*score+=1

标签: c function while-loop


【解决方案1】:

这行得通:

#include <stdio.h>
#include <stdlib.h>

void t(int *score1,int *score2)
{
    int r,p;
    r=rand() % 140;
    printf("%d\n", r);
    if(r>30)
    {
        *score1+=1; //increase score1 and not pointer of score1
    }
    else
    {
        *score2+=1; //increase score2 and not pointer of score2
    }

}

void main(int argc, char *argv[])
{
    srand(time(NULL));
    int score1=0, score2=0;
    int x =0;
    while(x==0)
    {
        t(&score1, &score2);
        printf("%d ",score1);
        printf("%d\n",score2);

        if(score1 > 3 && score1==score2+2)
        {
            printf("Player 1 Wins\n");
            x++;
        }
        if(score2 > 3 && (score2==score1+2))
        {
            printf("Player 2 Wins\n");
            x++;
        }
    }
    return;
}

【讨论】:

  • 你应该解释你改变了什么,以及原来的问题是什么。
【解决方案2】:

说明: 不要在 t 函数中调用 srand(time(NULL)) 。 srand() 根据时间创建一个种子,但如果你每次都调用它,在很短的时间内。见srand() — why call it only once?

代码:

#include <stdio.h>
#include <stdlib.h>

void t(int *score1,int *score2){
    int r,p;
    r=rand() % 140;

    printf("Random r: %d\n",r);
    if(r>30){
       (*score1)++;
    }
    else {
       (*score2)++;
    }
}

void main(int argc, char *argv[]){

int score1 =0;
int score2 =0;
int x =0;

srand(time(NULL));

while(x==0){
    t(&score1, &score2);
    printf("Score 1: %d\n",score1);
    printf("Score 2: %d\n",score2);

    if(score1 > 3 && score1==score2+2){
        printf("Player 1 Wins\n");
        x++;
    }

    if(score2 > 3 && (score2==score1+2)){
        printf("Player 2 Wins\n");
        x++;
    }
}

return;
}

样本输出:

Random r: 138
Score 1: 1
Score 2: 0
Random r: 2
Score 1: 1
Score 2: 1
Random r: 103
Score 1: 2
Score 2: 1
Random r: 26
Score 1: 2
Score 2: 2
Random r: 20
Score 1: 2
Score 2: 3
Random r: 12
Score 1: 2
Score 2: 4
Player 2 Wins

【讨论】:

  • 你应该解释你改变了什么,以及原来的问题是什么。
猜你喜欢
  • 2010-10-23
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
相关资源
最近更新 更多