PHP平均计算器无法正确显示结果

时间:2014-02-13 01:14:51

标签: php html function class object

我正在创建一个需要使用以下类的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();";

?>

错误信息: 警告:缺少参数1 for student :: average(),在C:\ Program Files(x86)\ EasyPHP-DevServer-14.1VC11 \ data \ localweb \ my portable files \ Exercise 4 \ average.php中调用第40行并定义在C:\ Program Files(x86)\ EasyPHP-DevServer-14.1VC11 \ data \ localweb \ my portable files \ Exercise 4 \ average.php 22行

注意:未定义的变量:第23行的C:\ Program Files(x86)\ EasyPHP-DevServer-14.1VC11 \ data \ localweb \ my portable files \ Exercise 4 \ average.php中的成绩

警告:在第30行的C:\ Program Files(x86)\ EasyPHP-DevServer-14.1VC11 \ data \ localweb \ my portable files \ Exercise 4 \ average.php中除以零

2 个答案:

答案 0 :(得分:1)

您似乎没有返回$ grade变量。它可能没有定义,因为你没有返回任何东西。

你的方法:

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实际添加到返回的数组中。

答案 1 :(得分:0)

以下是增强的类。您会注意到它们具有构造函数和函数来请求变量。私有变量通常是首选方法,因为它们受到保护,因此无法在类外修改。

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