user-0 提问: 无法统计  结果都是0；41；41；00那种  小白大一  求解答

回复:
user: user-1
content: 引用 8 楼 qq_40926715 的回复:[quote=引用 4 楼 cfjtaishan 的回复:] #include "stdio.h" #include "stdlib.h"//srand,random #include "time.h"//time(NULL) #define RANGENUM 100//控制随机数的范围为0-100 int main() {     int nRandNum;     int a,b,c,d,e;     a = 0;     b = 0;     c = 0;     d = 0;     e = 0;     srand(time(NULL));     for(int i = 0;i<100;i++)     {         nRandNum=rand()%RANGENUM;         printf("The %dth random num is: %d\n",i,nRandNum);         switch(nRandNum / 20)         {             case 0:     //(0<nRandNum<20);                 a++;                 break;              case 1:     //(21<nRandNum<40);                 b++;                 break;              case 2:    //(41<nRandNum<60);                 c++;                 break;              case 3:    //(61<nRandNum<80);                 d++;                 break;              case 4:     //(81<nRandNum<100);                 e++;                 break;                 /*                    case'1':                    a++;                    case'2':                    b++;                    case'3':                    c++;                    case'4':                    d++;                    case'5':                    e++;                    */         }     }     printf("the number of 0<nRandNum<20 is :%d\n",a);     printf("the number of 21<nRandNum<40 is :%d\n",b);     printf("the number of 41<nRandNum<60 is :%d\n",c);     printf("the number of 61<nRandNum<80 is :%d\n",d);     printf("the number of 81<nRandNum<100 is :%d\n",e);      return 0; } 参考一下吧； '1'和数值1是有区别的。'1' - 1 = 48;即'1'十进制是49； rand() 得到的是数值；不是字符；    e的输出值为0.不知道怎么回事[/quote] 看一下随机值有80~100之间的吗？ 有可能得到随机值没有80~100之间

user: user-2
content: #include "stdafx.h" #include "stdio.h" #include "stdlib.h"//srand,random  #include "time.h"//time(NULL) #define RANGENUM 100//控制随机数的范围为0-100 int main() { 	int nRandNum; 	srand(time(NULL)); // 让每次产生的随机数都不一样   	int a, b, c, d, e; 	a = b = c = d = e = 0; 	for (int i = 0; i < 100; i++)  //产生10个随机数 	{ 		nRandNum = rand() % RANGENUM; 		printf("The %dth random num is: %d\n", i, nRandNum);//生成随机数，并利用取余数控制数值范围  		if (nRandNum <= 20) a++; 		else if (nRandNum <= 40) b++; 		else if (nRandNum <= 60) c++; 		else if (nRandNum <= 80) d++; 		else e++; 	} 	printf("the number of 0<nRandNum<20 is :%d\n", a); 	printf("the number of 21<nRandNum<40 is :%d\n", b); 	printf("the number of 41<nRandNum<60 is :%d\n", c); 	printf("the number of 61<nRandNum<80 is :%d\n", d); 	printf("the number of 81<nRandNum<100 is :%d\n", e); 	 	return 0;  }

user: user-3
content: case 和'1'等等之间需要加个空格

user: user-0
content: 引用 4 楼 cfjtaishan 的回复:#include "stdio.h" #include "stdlib.h"//srand,random #include "time.h"//time(NULL) #define RANGENUM 100//控制随机数的范围为0-100 int main() {     int nRandNum;     int a,b,c,d,e;     a = 0;     b = 0;     c = 0;     d = 0;     e = 0;     srand(time(NULL));     for(int i = 0;i<100;i++)     {         nRandNum=rand()%RANGENUM;         printf("The %dth random num is: %d\n",i,nRandNum);         switch(nRandNum / 20)         {             case 0:     //(0<nRandNum<20);                 a++;                 break;              case 1:     //(21<nRandNum<40);                 b++;                 break;              case 2:    //(41<nRandNum<60);                 c++;                 break;              case 3:    //(61<nRandNum<80);                 d++;                 break;              case 4:     //(81<nRandNum<100);                 e++;                 break;                 /*                    case'1':                    a++;                    case'2':                    b++;                    case'3':                    c++;                    case'4':                    d++;                    case'5':                    e++;                    */         }     }     printf("the number of 0<nRandNum<20 is :%d\n",a);     printf("the number of 21<nRandNum<40 is :%d\n",b);     printf("the number of 41<nRandNum<60 is :%d\n",c);     printf("the number of 61<nRandNum<80 is :%d\n",d);     printf("the number of 81<nRandNum<100 is :%d\n",e);      return 0; } 参考一下吧； '1'和数值1是有区别的。'1' - 1 = 48;即'1'十进制是49； rand() 得到的是数值；不是字符；    OK了  刚才case是从12345  没有从0开始  所以最后没值 ，但开始那种为什么判断不能累加呢   谢谢你了

user: user-0
content: 引用 4 楼 cfjtaishan 的回复:#include "stdio.h" #include "stdlib.h"//srand,random #include "time.h"//time(NULL) #define RANGENUM 100//控制随机数的范围为0-100 int main() {     int nRandNum;     int a,b,c,d,e;     a = 0;     b = 0;     c = 0;     d = 0;     e = 0;     srand(time(NULL));     for(int i = 0;i<100;i++)     {         nRandNum=rand()%RANGENUM;         printf("The %dth random num is: %d\n",i,nRandNum);         switch(nRandNum / 20)         {             case 0:     //(0<nRandNum<20);                 a++;                 break;              case 1:     //(21<nRandNum<40);                 b++;                 break;              case 2:    //(41<nRandNum<60);                 c++;                 break;              case 3:    //(61<nRandNum<80);                 d++;                 break;              case 4:     //(81<nRandNum<100);                 e++;                 break;                 /*                    case'1':                    a++;                    case'2':                    b++;                    case'3':                    c++;                    case'4':                    d++;                    case'5':                    e++;                    */         }     }     printf("the number of 0<nRandNum<20 is :%d\n",a);     printf("the number of 21<nRandNum<40 is :%d\n",b);     printf("the number of 41<nRandNum<60 is :%d\n",c);     printf("the number of 61<nRandNum<80 is :%d\n",d);     printf("the number of 81<nRandNum<100 is :%d\n",e);      return 0; } 参考一下吧； '1'和数值1是有区别的。'1' - 1 = 48;即'1'十进制是49； rand() 得到的是数值；不是字符；    e的输出值为0.不知道怎么回事

user: user-0
content: 还是无法计数  已将 abcde初始化

user: user-4
content: 不适合用switch，且:0<nRandNum<20逻辑不对，相当于:(0<nRandNum)<20,即先算0<nRandNum的结果（为真或假）然后再和20进行比较，整个结果必经为真 #include <stdio.h> #include <stdlib.h>  //srand,random  #include <time.h>    //time(NULL) #define RANGENUM 100 //控制随机数的范围为0-100 int main() { 	int i, nRandNum;      int a = 0, b = 0, c = 0, d = 0, e = 0;  	srand(time(NULL)); // 让每次产生的随机数都不一样   	 	for (i = 0; i < 100; i++)  //产生100个随机数 	{ 		nRandNum = rand() % RANGENUM; 		printf("The %dth random num is: %d\n", i, nRandNum); //生成随机数,并利用取余数控制数值范围	        if (0 <= nRandNum && nRandNum <= 20)       {          ++a;       }       else if (21 <= nRandNum && nRandNum <= 40)       {          ++b;       }       else if (41 <= nRandNum && nRandNum <= 60)       {          ++c;       }       else if (61 <= nRandNum && nRandNum <= 80)       {          ++d;       }       else if (81 <= nRandNum && nRandNum <= 100)       {          ++e;       } 	}  	printf("the number of 0<nRandNum<20 is :%d\n", a); 	printf("the number of 21<nRandNum<40 is :%d\n", b); 	printf("the number of 41<nRandNum<60 is :%d\n", c); 	printf("the number of 61<nRandNum<80 is :%d\n", d); 	printf("the number of 81<nRandNum<100 is :%d\n", e);  	return 0;  }

user: user-1
content: 引用 3 楼 hefashion0190 的回复:#include "stdio.h" #include "stdlib.h"//srand,random  #include "time.h"//time(NULL) #define RANGENUM 100//控制随机数的范围为0-100 int main() { 	int nRandNum; 	srand(time(NULL)); // 让每次产生的随机数都不一样   	int a = 0, b = 0, c = 0, d = 0, e = 0; 	for (int i = 0; i < 100; i++)  //产生100个随机数 	{ 		nRandNum = rand() % RANGENUM; 		printf("The %dth random num is: %d\n", i, nRandNum);//生成随机数，并利用取余数控制数值范围 		switch (nRandNum) 		{ 		case'1':(0 < nRandNum < 20); 			a++; 		case'2':(21 < nRandNum < 40); 			b++; 		case'3':(41 < nRandNum < 60); 			c++; 		case'4':(61 < nRandNum < 80); 			d++; 		case'5':(81 < nRandNum < 100); 			e++; 		} 	}  	printf("the number of 0<nRandNum<20 is :%d\n", a); 	printf("the number of 21<nRandNum<40 is :%d\n", b); 	printf("the number of 41<nRandNum<60 is :%d\n", c); 	printf("the number of 61<nRandNum<80 is :%d\n", d); 	printf("the number of 81<nRandNum<100 is :%d\n", e);  	return 0;  } nRandNum不是字符吧，switch中的'1', '2', '3'，’4‘表示的是49， 50， 51， 52

user: user-1
content: #include "stdio.h" #include "stdlib.h"//srand,random #include "time.h"//time(NULL) #define RANGENUM 100//控制随机数的范围为0-100 int main() {     int nRandNum;     int a,b,c,d,e;     a = 0;     b = 0;     c = 0;     d = 0;     e = 0;     srand(time(NULL));     for(int i = 0;i<100;i++)     {         nRandNum=rand()%RANGENUM;         printf("The %dth random num is: %d\n",i,nRandNum);         switch(nRandNum / 20)         {             case 0:     //(0<nRandNum<20);                 a++;                 break;              case 1:     //(21<nRandNum<40);                 b++;                 break;              case 2:    //(41<nRandNum<60);                 c++;                 break;              case 3:    //(61<nRandNum<80);                 d++;                 break;              case 4:     //(81<nRandNum<100);                 e++;                 break;                 /*                    case'1':                    a++;                    case'2':                    b++;                    case'3':                    c++;                    case'4':                    d++;                    case'5':                    e++;                    */         }     }     printf("the number of 0<nRandNum<20 is :%d\n",a);     printf("the number of 21<nRandNum<40 is :%d\n",b);     printf("the number of 41<nRandNum<60 is :%d\n",c);     printf("the number of 61<nRandNum<80 is :%d\n",d);     printf("the number of 81<nRandNum<100 is :%d\n",e);      return 0; } 参考一下吧； '1'和数值1是有区别的。'1' - 1 = 48;即'1'十进制是49； rand() 得到的是数值；不是字符；

user: user-5
content: #include "stdio.h" #include "stdlib.h"//srand,random  #include "time.h"//time(NULL) #define RANGENUM 100//控制随机数的范围为0-100 int main() { 	int nRandNum; 	srand(time(NULL)); // 让每次产生的随机数都不一样   	int a = 0, b = 0, c = 0, d = 0, e = 0; 	for (int i = 0; i < 100; i++)  //产生100个随机数 	{ 		nRandNum = rand() % RANGENUM; 		printf("The %dth random num is: %d\n", i, nRandNum);//生成随机数，并利用取余数控制数值范围 		switch (nRandNum) 		{ 		case'1':(0 < nRandNum < 20); 			a++; 		case'2':(21 < nRandNum < 40); 			b++; 		case'3':(41 < nRandNum < 60); 			c++; 		case'4':(61 < nRandNum < 80); 			d++; 		case'5':(81 < nRandNum < 100); 			e++; 		} 	}  	printf("the number of 0<nRandNum<20 is :%d\n", a); 	printf("the number of 21<nRandNum<40 is :%d\n", b); 	printf("the number of 41<nRandNum<60 is :%d\n", c); 	printf("the number of 61<nRandNum<80 is :%d\n", d); 	printf("the number of 81<nRandNum<100 is :%d\n", e);  	return 0;  }

user: user-5
content: a,b,c,d,e未初始化  int a=0,b=0,c=0,d=0,e=0;

user: user-6
content: rand()产生的数是比1小的小数去%RANGENUM没问题？