创建变化的变量

标签 c loops variables c99

这是我的部分代码:

float score_from_judge(number_judges, n) 
{ 
   float judge_score;    
   printf("Score from judge %d? ", n);
   scanf("%f", &judge_score);

   return(judge_score);
}

int main(void) 
{
   int n;
   float judge_score;
   n=1;
   for(n; n<=number_judges; n++ ) {
      judge_score = score_from_judge(number_judges, n)
   }
}

每次 score_from_judge 返回值时,我正在寻找一种将 judge_score 保存在变量中的方法。 我试图找到一种方法来为每个循环创建一个新变量。

例如,我第一次收到 judge_score 时,它​​保存在 variable_1 中,下一次保存在 variable_2 中,等等。

最佳答案

声明一个 float 组并在“for”循环中设置它的值,如下所示:

int main(){
    float judge_score[10];
    for(int n=1; n<=number_judges; n++ ) {
        judge_score[n] = score_from_judge(number_judges, n);
    }
}

关于创建变化的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853348/

相关文章:

c++ - C 和 C++ 中 printf ("%d\n"、 sizeof ('a' )) 的结果是什么

c - 弄清楚这个按位 AND 和 LSL 在 C 中到底做了什么

c - 在 f# 中编写方法

python - numpy 中的 "as of"

php - SQL查询不读取变量

c++ - 如何检查一个变量是否不等于 C++ 中的多个东西?

c - 我们可以在 `fdopen()` 中指定哪些类型,以及该类型的哪一部分是有效的?

javascript - 当ajax结束时jQuery继续循环

loops - gdb:循环内的 SIGFPE

c - 在 C 中检测/列出变量声明