我正在尝试请求 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/