作者Yagyu (マタアイマショウ )
看板Fortran
标题Re: [问题] IF ELSE
时间Wed Oct 21 22:20:33 2009
※ 引述《ilelm (我想吃好蛋)》之铭言:
: 最近学到了if else语法,遇到了一个问题,可以请各位帮我看问题出在哪里吗?谢谢!
: 题目是这样:
: The cost of sending apackage by an express delivery service is $10.00
: for the first 2 pounds,and $3.75 for each pound or fraction thereof
: 2pounds.If the package weighs more than 70pounds, a $10.00 excess
: weight surcharge is added to the cost.No package over 100 pounds
: will be accepted.Write aprogram that accept the weight of a pcckage
: in pounds and computesthe cost of mailing the package. Be sure to
: handle the case ofoverweight packages.
: 我写的程式如下:
: program main
: implicit none
: real :: w,cost,a,b,c
: write(*,*)"请输入包裹的重量(单位:磅)"
: read(*,*) w
: if (w>100.0) then
: write(*,*)"超重了你知道吗?"
: else if(w<=2.0) then
: a=10.00
: write(*,*)"cost=",a
: else if (2.0 <w <=70.0.AND.INT(W-2.0)=W-2.0) then
: b=10.0+3.75(w-2.0)
: write(*,*)"cost=",b
: else if (2.0 < w < 70.0 .AND.INT(W-2.0)/=W-2.0) then
: b=10.0+3.75(int(w+1.0)-2.0)
: write(*,*)"cost=",b
: else if(w>70.0 .AND.INT(W-2.0)=W-2.0) then
: c=20.0+3.75(w-2.0)
: else if (w>70.0 .AND.INT(W-2.0)/=W-2.0) then
: c=20.0+3.75(int(w+1.0)-2.0)
: write(*,*)"cost=",c
: end if
: pause
: stop
: end program main
单纯的只看语法的话,红色的部分都是不正确的...
所以我改了一下...
program main
implicit none
real :: w, cost
write (*,*) '您的包裹重量是(单位:磅)?'
read (*,*) w
if (w < 0.0) then
write (*,*) '这里面装的是阿飘吗?'
else if (w <= 2.0) then
write (*,*) '包裹重量在 2 磅以下, 运用费用 $10.0'
else if (2.0 < w .and. w < 70.0) then
cost = 10.0+3.75*int(w-2.0)
write (*,*) '包裹重量为', w, '磅, 运用费用 $', cost
else if (70 <= w .and. w < 100.0) then
cost = 10.0+3.75*int(w-2.0)+10.0
write (*,*) '包裹重量为', w, '磅, 运用费用 $', cost
else if (100.0 <= w) then
write (*,*) '抱歉, 我们不收100磅以上包裹.'
end if
pause
stop
end program main
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 219.84.1.2