将数据储存在数组中(数组赋值问题),然后操作
程序代码:
#include <ctype.h> #include <stdio.h> #define ALL_SUM 26 int main(void) { int sum, j, i = 0, a[ALL_SUM] = {0}; char ch; printf("Enter a word: "); while ((ch = getchar()) != '\n') switch (toupper(ch)) { case 'D': case 'G': a[i]= 2; i++; break; case 'B': case 'C': case 'M': case 'P': a[i]= 3; i++; break; case 'F': case 'H': case 'V': case 'W': case 'Y': a[i]= 4; i++; break; case 'K': a[i]= 5; i++; break; case 'J': case 'X': a[i]= 8; i++; break; case 'Q': case 'Z': a[i]= 10;i++; break; default: a[i] = 1; i++; break; } for (j = 0; j < i; j++) sum += a[i]; printf("Scrabble value: %d\n", sum); return 0; }
很简单的一个程序,输入字母,相应的转化为数字和。看下数组途中赋值出现了什么问题呢?初始化赋值为0,把后续赋值遮盖还是后面赋值本身就是错误?