c - 我无法让循​​环循环 20 次并要求输入 1 到 6 之间的数字。任何人都可以看到我编码错误的地方吗?

标签 c

我正在尝试请求 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 times\n You entered the number five %d times\n", 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 语句的执行。控制传递给终止语句之后的语句。

删除所有的中断。

还学习使用调试器。

google:“如何调试 C 代码”和您的 IDE 的名称 - 您用来编写代码的程序。

关于c - 我无法让循​​环循环 20 次并要求输入 1 到 6 之间的数字。任何人都可以看到我编码错误的地方吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31273007/

相关文章:

c - 如何根据传递的结构名称部分访问结构

c - 无法证明输出的合理性

c - 指向结构的指针

c - 为什么 gdb 看不到 `stdio` 何时更改?

ios - 如何在 iOS 中获得 C 函数崩溃?

c++ - 作为业余爱好者学习编程...C 与 C++ 的优点

c - 如何在risc-v中实现printf函数?

c - C linux查询MX记录

c - 为什么 MPI_Sendrecv 会阻塞?

c - 用c语言来说。为什么看起来 system() 先于 printf() 被调用?