<?php
$VarC = 6;
$VarD = 2;
$ctA = 1;
$ctB = 2;
$VarAns = 0;
$VarAns = $ctA * ($ctB+$VarC) / $VarD;
do {
do {
$VarAns = $ctA * ($ctB+$VarC) / $VarD;
echo ("X = $ctA * ($ctB + 6) / 2 is equal to: $VarAns<br>");
$ctA = $ctA + 1;
} while ($ctA <= 10);
echo ("<br>");
$ctA = 1;
$ctB = $ctB + 2;
$VarAns = $ctA * ($ctB+$VarC) / $VarD;
echo ("X = $ctA * ($ctB + 6) / 2 is equal to: $VarAns<br>");
$ctA = $ctA + 1;
} while ($ctB <= 10);
?>
我最后得到一条额外的线,我不应该得到任何想法? 我试图打印并在我达到10时停止,但它会变为12!
我得到了
X = 1 * (2 + 6) / 2 is equal to: 4
X = 2 * (2 + 6) / 2 is equal to: 8
X = 3 * (2 + 6) / 2 is equal to: 12
X = 4 * (2 + 6) / 2 is equal to: 16
X = 5 * (2 + 6) / 2 is equal to: 20
X = 6 * (2 + 6) / 2 is equal to: 24
X = 7 * (2 + 6) / 2 is equal to: 28
X = 8 * (2 + 6) / 2 is equal to: 32
X = 9 * (2 + 6) / 2 is equal to: 36
X = 10 * (2 + 6) / 2 is equal to: 40
X = 1 * (4 + 6) / 2 is equal to: 5
X = 2 * (4 + 6) / 2 is equal to: 10
X = 3 * (4 + 6) / 2 is equal to: 15
X = 4 * (4 + 6) / 2 is equal to: 20
X = 5 * (4 + 6) / 2 is equal to: 25
X = 6 * (4 + 6) / 2 is equal to: 30
X = 7 * (4 + 6) / 2 is equal to: 35
X = 8 * (4 + 6) / 2 is equal to: 40
X = 9 * (4 + 6) / 2 is equal to: 45
X = 10 * (4 + 6) / 2 is equal to: 50
X = 1 * (6 + 6) / 2 is equal to: 6
X = 2 * (6 + 6) / 2 is equal to: 12
X = 3 * (6 + 6) / 2 is equal to: 18
X = 4 * (6 + 6) / 2 is equal to: 24
X = 5 * (6 + 6) / 2 is equal to: 30
X = 6 * (6 + 6) / 2 is equal to: 36
X = 7 * (6 + 6) / 2 is equal to: 42
X = 8 * (6 + 6) / 2 is equal to: 48
X = 9 * (6 + 6) / 2 is equal to: 54
X = 10 * (6 + 6) / 2 is equal to: 60
X = 1 * (8 + 6) / 2 is equal to: 7
X = 2 * (8 + 6) / 2 is equal to: 14
X = 3 * (8 + 6) / 2 is equal to: 21
X = 4 * (8 + 6) / 2 is equal to: 28
X = 5 * (8 + 6) / 2 is equal to: 35
X = 6 * (8 + 6) / 2 is equal to: 42
X = 7 * (8 + 6) / 2 is equal to: 49
X = 8 * (8 + 6) / 2 is equal to: 56
X = 9 * (8 + 6) / 2 is equal to: 63
X = 10 * (8 + 6) / 2 is equal to: 70
X = 1 * (12 + 6) / 2 is equal to: 9
最后一行X = 1 * (12 + 6) / 2 is equal to: 9
不应该在那里
答案 0 :(得分:1)
问题不在于while循环,而是你的初始代码不起作用,你使用了一个技巧。
嵌套的do / while在第一个主do / while循环中生成10行,但在后续循环中只生成9行,因此在关闭主循环之前添加了一个额外的echo
以“预期”下一个循环:此回声甚至在最后一个循环中执行,然后是额外的意外行。
只需更改$ctA = 1;
中的$ctA = 0;
(在循环中)并删除最终的echo
:
$VarC = 6;
$VarD = 2;
$ctA = 1;
$ctB = 2;
$VarAns = 0;
$VarAns = $ctA * ($ctB+$VarC) / $VarD;
do {
do {
$VarAns = $ctA * ($ctB+$VarC) / $VarD;
echo ("X = $ctA * ($ctB + 6) / 2 is equal to: $VarAns<br>");
$ctA = $ctA + 1;
} while ($ctA <= 10);
echo ("<br>");
$ctA = 0; // <----------------
$ctB = $ctB + 2;
$VarAns = $ctA * ($ctB+$VarC) / $VarD;
$ctA = $ctA + 1;
} while ($ctB <= 10);
的 eval.in demo 强>
答案 1 :(得分:0)
而不是
do {
....
} while (...);
做的:
while(..){
....
}
答案 2 :(得分:0)
你确定它打印下面的行吗?
X = 10 * (8 + 6) / 2 is equal to: 70
X = 1 * (12 + 6) / 2 is equal to: 9
因为您的代码应输出:
X = 10 * (8 + 6) / 2 is equal to: 70
X = 1 * (10 + 6) / 2 is equal to: 8
X = 2 * (10 + 6) / 2 is equal to: 16
...
X = 10 * (10 + 6) / 2 is equal to: 80
X = 1 * (12 + 6) / 2 is equal to: 9
查看$ctA == 1
和$ctB == 8
时的情况。执行时输入:
do {
$VarAns = $ctA * ($ctB+$VarC) / $VarD;
echo ("X = $ctA * ($ctB + 6) / 2 is equal to: $VarAns<br>");
$ctA = $ctA + 1;
} while ($ctA <= 10);
它将打印:
X = 1 * (8 + 6) / 2 is equal to: 7
...
X = 10 * (8 + 6) / 2 is equal to: 70
在内循环的最后一次迭代$ctA == 10
,所以在增加1之后,现在$ctA == 11
,然后它退出内部do .. while
。接下来,它进入:
echo ("<br>");
$ctA = 1;
$ctB = $ctB + 2;
它打印另一个中断行,将$ ctA值重置为1.将$ ctB增加2.所以现在$ctA == 1
和$ctB == 10
进入:
$VarAns = $ctA * ($ctB+$VarC) / $VarD;
echo ("X = $ctA * ($ctB + 6) / 2 is equal to: $VarAns<br>");
$ctA = $ctA + 1;
打印:
X = 1 * (10 + 6) / 2 is equal to: 8
接下来,它将$ ctA值增加1,现在$ctA == 2
和$ctB == 10
。因此条件($ctB <= 10)
变为真,外do .. while
再次重复。然后它进入内循环并打印:
X = 2 * (10 + 6) / 2 is equal to: 8
...
X = 10 * (10 + 6) / 2 is equal to: 80
它退出内循环并打印另一个断行和最后一个输出
X = 1 * (12 + 6) / 2 is equal to: 9
问题是多次调用此代码:
echo ("X = $ctA * ($ctB + 6) / 2 is equal to: $VarAns<br>");
并且不会将$ ctA重置为0.