0

谁能帮我理解如何通过 for 循环传递变量?我想打印出变量 $guess 直到数字 40 并打印出“你猜低了”或“你猜得太高了”......我如何将变量与计数器连接起来?

$Me=30;
$guess=$i;

for ($i = 0; $i < 40; $i++) {
    if($Me>$guess){
    print "$guess:you guessed too low<br />";
    }elseif($Me<$guess){
    print "$guess: you guessed to high<br />";
    }else{
    print "you guessed my age! i'm 31!";
    }
}
4

4 回答 4

1
$Me=30;

for ($i = 0; $i < 40; $i++) {
$guess=$i; // move it here
    if($Me>$guess){
    print "$guess:you guessed too low<br />";
    }elseif($Me<$guess){
    print "$guess: you guessed to high<br />";
    }else{
    print "you guessed my age! i'm 31!";
    }
}
于 2013-10-11T00:50:56.477 回答
1

在 for 循环中,将 $i 替换为 $guess。

但是,尽管 $Me=30,程序仍会打印“i'm 31”。

于 2013-10-11T00:55:40.817 回答
1
$Me=30;
/* actually you don't need to use $guess=$i; for your needs */ 
for($i = 0; $i < 40; $i++) {
    if($i < $Me){
        echo "$i:you guessed too low <br/>";
    }
    else if($i > $Me){
        echo "$i: you guessed to high<br/>";
    }
    else{
        echo "you guessed my age! i'm 31!";
    }
}
于 2013-10-11T01:02:30.197 回答
1
$Me=30;

for ($i = 0; $i < 40; ) {
     $guess=$i;
    if($Me>$guess){
        print "$guess:you guessed too low<br />";
    }elseif($Me<$guess){
        print "$guess: you guessed to high<br />";
    }else{
        print "you guessed my age! i'm 31!";
    }
$i++;
}
于 2013-10-11T01:04:13.090 回答