PHP代码错误。需要使用数组和for循环

我可以说我在这里犯了一个非常愚蠢的错误。有人可以通过更正我的代码来帮助我吗?可能是我的循环有问题。


<?php


        $students = array("Sauer Jeppe", "Von Weilligh", "Troy Commisioner", "Paul Krugger", "Jacob Maree");

        $grades = array(75, 44, 60, 62, 70);


        for($i=0; $i<count($students); $i++){


            for($j=0; $j<count($grades); $j++){



                if($grades[$j] >= 70){


                    echo"$students[$i] scored a Distinction.";

                }


                elseif($grades[$j] >= 50){


                    echo"$students[$i] scored a Pass.";

                }


                elseif($grades[$j] >= 0){


                    echo"$students[$i] scored a Fail.";

                }


            }


        }


    ?>

它的目的是显示:


Sauer Jeppe 获得了优异奖。


Von Weilligh 成绩不及格。


特洛伊专员获得了通过。


保罗·克鲁格(Paul Krugger)传球。


Jacob Maree 获得了优异成绩。


谢谢你。


拉莫斯之舞
浏览 99回答 1
1回答

弑天下

不需要内部循环,只需从同一个键下for取值即可:$grades$ifor($i=0; $i<count($students); $i++){&nbsp; &nbsp; $grade = $grades[$i];&nbsp; &nbsp; if($grade >= 70){&nbsp; &nbsp; &nbsp; &nbsp; echo"$students[$i] scored a Distinction.";&nbsp; &nbsp; } elseif ($grade >= 50){&nbsp; &nbsp; &nbsp; &nbsp; echo"$students[$i] scored a Pass.";&nbsp; &nbsp; } elseif ($grade >= 0){&nbsp; &nbsp; &nbsp; &nbsp; echo"$students[$i] scored a Fail.";&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP