cycle无法根据if条件进行判断
在这个程序的cycle部分,我想实现 : 如果x在0和1之间就退出循环,否则不断要求输入x和n。编译成功,但输入正确后,仍然循环,如输入0.5100。恳请大家指教^^IVF, Win7, fortran 95
program ln
implicit none
real::x,term,sum=0
integer::n
do
print*,'please input x and n'
read*,x,n
if(x>1.or.x<0.000001) cycle
end do
do
term=(x**n)/n
sum=sum+term
n=n-1
if(n==0) exit
end do
print*,sum
end program ln
第一个循环没有退出条件,所以会一直循环下去,也就是不停地要求输入x,n if(x>1.or.x<0.000001) cycle
改为
if(x<=1.and.x>0.000001) exit 百事可乐 发表于 2014-10-17 09:40
if(x>1.or.x
没法用cycle实现吗? Shiev 发表于 2014-10-17 14:48
没法用cycle实现吗?
cycle 是执行下一次do循环,而您的do循环是无限循环,那么cycle肯定是跳不出去的,exit是跳出本层的do循环 hanshikai 发表于 2014-10-17 08:03
第一个循环没有退出条件,所以会一直循环下去,也就是不停地要求输入x,n
有退出条件啊,不满足if里的条件不就退出了吗? 本帖最后由 楚香饭 于 2014-10-17 16:03 编辑
你没有写 else exit
if 的意思是,如果。而不是:只有
楚香饭 发表于 2014-10-17 16:00
你没有写 else exit
if 的意思是,如果。而不是:只有
sorry, can you please give me a modified version? and I still don't understand the syntax of "circle"
xue xiao mei you zhong wen shu ru fa, bu hao yi si la^^, program ln
implicit none
real::x,term,sum=0
integer::n
do
print*,'please input x and n'
read*,x,n
if(x>1.or.x<0.000001) then
cycle
else
exit
end if
end do
do
term=(x**n)/n
sum=sum+term
n=n-1
if(n==0) exit
end do
print*,sum
end program ln 楚香饭 发表于 2014-10-17 16:00
你没有写 else exit
if 的意思是,如果。而不是:只有
理解了,谢谢!!
页:
[1]