cI不能让循环循环20次并请求1到6之间的数字.有人能看出我写错了什么吗?

  • 本文关键字:循环 错了 数字 20次 不能 请求 之间 cI
  • 更新时间 :
  • 英文 :


我试图要求20个整数和计数,然后用静态变量选择数字2和5。这就是我使用代码块所能做的。它要求的不是20,而是1。

#include <stdio.h>
#include <stdlib.h>
int totalCount2(int ); *this is where i added the function call*
int totalCount5(int );
void output( int, int);
int main()
{
int count2;
int count5;
int yourNumber;
int yourNumberCounter;
yourNumberCounter = 1;
count2 =0;
count5 =0;
    printf("Please enter a number between 1 and 6.n");
    scanf("%d", &yourNumber);
while(yourNumberCounter<= 20)
    {
       if(yourNumber ==2){
        totalCount2(count2);
        break;
        }
        else if(yourNumber ==5){
            totalCount5(count5);
            break;
        }
        else if(yourNumber <= 6 || yourNumber >=1){
            yourNumberCounter = yourNumberCounter +1;
        }
        else if(yourNumber >6 || yourNumber <6){
            printf("You have to choose a number between 1 and 6. try again");
        }
    }
 return 0;
}
    int totalCount2(int count2){
        static int count2only;
        count2only = count2++;
        return count2only;
    }
    int totalCount5(int count5){
        static int count5only;
        count5only += count5;
        return count5only;
    }
    void output(count2, count5){
    printf("Out of the 20 numbers you input. You entered the number two %d timesn You entered the number five %d timesn", count2, count5);
    return;
}

我不确定我是否正确使用静态变量count2和count5。我正在学习一本书,想也许有人看到我做错了什么。

printf("Please enter a number between 1 and 6.n");
scanf("%d", &yourNumber);
while(yourNumberCounter<= 20)

在循环之外

应该

while(yourNumberCounter<= 20){
printf("Please enter a number between 1 and 6.n");
scanf("%d", &yourNumber);

break语句终止最近的do、for、switch或while语句的执行。控制传递给终止语句后面的语句。

删除所有的break

也要学会使用调试器

google: "how to debug c code"和你的IDE的名称-你用来写代码的程序。

最新更新