PHP平均计算器不能正确显示结果
我正在创建一个需要使用以下类的php计算器,然后打印出用户名和他们获得的平均分数。 这是我到目前为止的代码,但它不能正确显示,它是说缺少参数和未定义的变量,但我不知道我错了哪里!
<?php
class person {
public $name;
}
class student extends person {
function student ($name, $grade1, $grade2) {
if(is_numeric($grade1) && is_numeric($grade2)){
$grades = array($grade1, $grade2);
}
elseif (is_numeric($grade1)) {
$grade2 = 0;
}
else {
$grade1 = 0;
}
}
function average ($grades) {
$length = count($grades);
$total = 0;
for ($i = 0; $i < $length; $i++) {
$total = $total + $grades[i];
}
$average = $total / $length;
return $average;
}
}
$person1 = new student ($_POST['firstName'], $_POST['firstGrade1'], $_POST['firstGrade2']);
$person2 = new student ($_POST['secondName'], $_POST['secondGrade1'], $_POST['secondGrade2']);
$person3 = new student ($_POST['thirdName'], $_POST['thirdGrade1'], $_POST['thirdGrade2']);
echo "<br/> $person1->name" . "achieved an average of" . "$person1->average();";
echo "<br/> $person2->name" . "achieved an average of" . "$person2->average();";
echo "<br/> $person3->name" . "achieved an average of" . "$person3->average();";
?>
错误消息:警告:在学生:: average()中缺少参数1,在C: Program Files(x86) EasyPHP-DevServer-14.1VC11 data localweb my portable files Exercise 4 average.php中调用40,并在第22行的C: Program Files(x86) EasyPHP-DevServer-14.1VC11 data localweb my portable files Exercise 4 average.php中定义
注意:未定义的变量:在C: Program Files(x86) EasyPHP-DevServer-14.1VC11 data localweb 我的便携文件 Exercise 4 average.php中的等级在23行
警告:在C: Program Files(x86) EasyPHP-DevServer-14.1VC11 data localweb 我的便携文件 Exercise 4 average.php中对零进行划分
您似乎没有返回$等级变量。 它可能没有定义,因为你没有返回任何东西。
你的方法:
function student ($name, $grade1, $grade2) {
if(is_numeric($grade1) && is_numeric($grade2)){
$grades = array($grade1, $grade2);
}
elseif (is_numeric($grade1)) {
$grade2 = 0;
$grades = array($grade1, $grade2);
}
else {
$grade1 = 0;
$grades = array($grade1, $grade2);
}
return $grades
}
需要看起来更像这样。 您还需要在您的备用条件中实际将grade1和grad2添加到返回的数组中。
以下是增强的类。 你会注意到它们有构造函数和函数来请求变量。 私有变量通常是首选方法,因为它们受到保护,因此不能在课程外进行修改。
class person {
private $name = "";
public function __construct ($nameV) {
$this->name = $nameV;
}
public function getName() {
return $this->name;
}
}
class student extends person {
private $grades;
public function __construct ($name, $grade1, $grade2) {
parent::__construct($name);
if ( ! is_numeric($grade1)) { $grade1 = 0; }
if ( ! is_numeric($grade2)) { $grade2 = 0; }
$this->grades = array($grade1, $grade2);
}
public function average () {
$length = count($this->grades);
$total = 0;
for ($i = 0; $i < $length; $i++) {
$total = $total + $this->grades[$i];
}
$average = $total / $length;
return $average;
}
}
出口代码然后简单地:
echo "<br/>" . $person1->getName() . " achieved an average of " . $person1->average();
echo "<br/>" . $person2->getName() . " achieved an average of " . $person2->average();
echo "<br/>" . $person3->getName() . " achieved an average of " . $person3->average();
由于没有表格,我使用以下数据进行了测试:
$person1 = new student ("XYZ", 1, 2);
$person2 = new student ("XYZ2", 100, 20);
$person3 = new student ("XYZ3", 95, 94);
导出的是:
XYZ achieved an average of 1.5
XYZ2 achieved an average of 60
XYZ3 achieved an average of 94.5
链接地址: http://www.djcxy.com/p/69531.html