代码有问题吗?

ztcreative 2006-02-28 12:11:08
#include<iostream.h>
void main()
{
char c;
int i;
int nother,ndigit[10];
for (i=0;i<10;i++)
ndigit[i]=0;
cin>>c;
while(c!='$')
switch(c)
{
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
++ndigit[c-'0'];break;
default:++nother;
}
cout<<"ndigit=";
for(i=0;i<10;i++)
cout<<ndigit[i]<<' ';
cout<<"\nother="<<nother<<endl;
}
统计各种数字个数和其他字符的个数
我感觉这个代码没问题 为什么不能输出正确的结果?
...全文
105 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jlxys 2006-02-28
  • 打赏
  • 举报
回复
void main()
{
char c;
int i, num = 0;
int nother = 0,ndigit[10];
for (i=0;i<10;i++)
ndigit[i]=0;

while(cin>>c,c!='$'&¬her<10&&num++<10)
switch(c)
{
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
++ndigit[c-'0'];break;
default:++nother;
}
cout<<"ndigit=";
for(i=0;i<10;i++)
cout<<ndigit[i]<<' ';
cout<<"\nother="<<nother<<endl;
}
jlxys 2006-02-28
  • 打赏
  • 举报
回复
void main()
{
char c;
int i;
int nother,ndigit[10];
for (i=0;i<10;i++)
ndigit[i]=0;

while(cin>>c,c!='$')
switch(c)
{
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
++ndigit[c-'0'];break;
default:++nother;
}
cout<<"ndigit=";
for(i=0;i<10;i++)
cout<<ndigit[i]<<' ';
cout<<"\nother="<<nother<<endl;
}
hai_li 2006-02-28
  • 打赏
  • 举报
回复
#include<iostream.h>
void main()
{
char c;
int i;
int nother,ndigit[10];
nother=0;
for (i=0;i<10;i++)
ndigit[i]=0;
while(cin>>c&&c!='$')
switch(c)
{
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
++ndigit[c-'0'];break;
default:++nother;
}
cout<<"ndigit=";
for(i=0;i<10;i++)
cout<<ndigit[i]<<' ';
cout<<"\nother="<<nother<<endl;
}
nextflying 2006-02-28
  • 打赏
  • 举报
回复
void main()
{
char c='s';
int i;
int nother=0,ndigit[10];
for (i=0;i<10;i++)
ndigit[i]=0;
while(c!='$')
{
cin>>c;
switch(c)
{
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
++ndigit[c-'0'];break;
default:++nother;
}
}
cout<<"ndigit=";
for(i=0;i<10;i++)
cout<<ndigit[i]<<' ';
cout<<"\nother="<<nother<<endl;
}

结果已经可以出来了,cin>>c;放在循环中就可以了。
jlxys 2006-02-28
  • 打赏
  • 举报
回复
领会错了 这下可以了

void main()
{
char c;
int i;
int nother = 0,ndigit[10];
for (i=0;i<10;i++)
ndigit[i]=0;

while(cin>>c,c!='$'&¬her<10)
switch(c)
{
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
++ndigit[c-'0'];break;
default:++nother;
}
cout<<"ndigit=";
for(i=0;i<10;i++)
cout<<ndigit[i]<<' ';
cout<<"\nother="<<nother<<endl;
}
ouyh12345 2006-02-28
  • 打赏
  • 举报
回复
循环层次不清,不太好看
while循环包括哪些?
ztcreative 2006-02-28
  • 打赏
  • 举报
回复
大虾 帮忙啊

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧