for循环小问题

SHOUYU2 2009-09-02 08:27:51
想用一个for循环,来找出重复的数,
找到了就提示被找到,没找到就输出没找到。
但是好像哪里出错,找到了,还是会接着输出“can't find the item "

void finditem(int number)
{
for(current=head;current!=NULL;current=current->next)
if(number==current->value)
puts("the item has been found");
printf("can't find the item %d.\n",number);
}
...全文
149 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
MichaelBomb 2009-09-03
  • 打赏
  • 举报
回复
if...
else...
yuio123465 2009-09-03
  • 打赏
  • 举报
回复
恩 楼上的基本都对啊
KopetwN 2009-09-03
  • 打赏
  • 举报
回复
用return可以

用break不行吧。。。。。。
alfredzz 2009-09-03
  • 打赏
  • 举报
回复
同意!
rejoice914 2009-09-03
  • 打赏
  • 举报
回复
顶楼上!我跟你观点一样!
younel0921 2009-09-03
  • 打赏
  • 举报
回复
都什么啊 whg01 的可以。其他的从for循环里break出来,不还是要执行
printf("can't find the item %d.\n",number);
吗?? break只是在找到之后不再往下循环而已。直接退出函数,或者判断是不是已经找到了,没找到再 printf("can't find the item %d.\n",number);
lihan6415151528 2009-09-03
  • 打赏
  • 举报
回复
break;
TTOJJ 2009-09-03
  • 打赏
  • 举报
回复
樓上的都是正解
wesleyluo 2009-09-03
  • 打赏
  • 举报
回复
楼上的都可以啊.
van_lin 2009-09-03
  • 打赏
  • 举报
回复
甚至还可以用 goto??

void finditem(int number)
{
for(current=head;current!=NULL;current=current->next)
if(number==current->value){
puts("the item has been found");
goto _ABC; //找到后就退出循环
}
_ABC:
printf("can't find the item %d.\n",number);
}
神经门首 2009-09-03
  • 打赏
  • 举报
回复
学习了
djwinter 2009-09-03
  • 打赏
  • 举报
回复
假如要重复找用 continue

假如后面还有代码用break

假如函数任务结束用return

假如想让程序借宿用exit

当然也可以用goto, 或者if
rejoice914 2009-09-03
  • 打赏
  • 举报
回复
就算找到了不是还要执行printf的吗!break是跳出for循环啊!我糊涂了
SHOUYU2 2009-09-03
  • 打赏
  • 举报
回复
代码贴出来一下:
#include <stdio.h>
#include <stdlib.h>
struct film //定义存储在链表里面的数据类型//
{
int value;
struct film *next;
};

struct film *head;
struct film *prev,*current;
//申明函数//
void createlist(); //生成链表//
void additem(); //添加项目//
void printlist(); //打印链表//
void freelist(); //清空链表//
void finditem(int number);

void createlist()
{
head=NULL; //设置头指针为空//
}

void additem()
{
int input;
puts("enter the value");
while(scanf("%d",&input)==1)
{
current=(struct film *)malloc(sizeof(struct film));
if(current==NULL)
puts("gao chui zi");
if(head==NULL) //只有一个头指针//
head=current; //写反了,在随机的宇宙里生成了一个链表的开始//
else prev->next=current;
current->next=NULL;
current->value=input;
prev=current;
puts("enter next number q to stop" ) ;
}
}

void printlist()
{
for(current=head;current!=NULL; current=current->next)
printf("the number is %d \n",current->value);
}

void freelist()
{
for(current=head;current!=NULL; current=current->next)
free(current);
}

void finditem(int number)
{
for(current=head;current!=NULL;current=current->next)

if(number==current->value){
puts("the item has been found");
return; //找到后就退出循环
}
printf("can't find the item %d.\n",number);

}



main()
{
createlist();
additem();
finditem(88);
freelist();
puts("bye");
}



  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fhtingtian 的回复:]
C/C++ codevoid finditem(int number)
{for(current=head;current!=NULL;current=current->next)if(number==current->value){
puts("the item has been found");break;//找到后就退出循环 }
printf¡­
[/Quote]
.
rejoice914 2009-09-03
  • 打赏
  • 举报
回复
晕!我多此一举!
SHOUYU2 2009-09-03
  • 打赏
  • 举报
回复
return,和exit(0)可以,break不可以,楼上的同学,用大括号还是不行
rejoice914 2009-09-03
  • 打赏
  • 举报
回复
void finditem(int number) 
{
for(current=head;current!=NULL;current=current->next)
{//这个地方加括号啊!
if(number==current->value){
puts("the item has been found");
break; //找到后就退出循环
}
printf("can't find the item %d.\n",number);
}
}



你们觉得printf不会每次执行吗???奇怪了!
每次都会执行printf的!
lijia_3636 2009-09-03
  • 打赏
  • 举报
回复
break , return

if(...)
{
puts("the item has been found");
}
else
{
printf("can't find the item %d.\n",number);

}
都可以
SHOUYU2 2009-09-03
  • 打赏
  • 举报
回复
为什么break不行,return可以呢?
加载更多回复(9)

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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