我的PHP代码是:
<?php
class Markov {
protected $arr0;
protected $arr1;
protected $arr2;
public $n;
public $p00;
public $p01;
public $p02;
public $p10;
public $p11;
public $p12;
public $p20;
public $p21;
public $p22;
public function __construct($p)
{
$arr0=new SplFixedArray(3);
$arr1=new SplFixedArray(3);
$arr2=new SplFixedArray(3);
$this->n=$p;
for($i=0; $i<3; $i++)
{
$arr0[$i]=0;
$arr1[$i]=0;
$arr2[$i]=0;
}
}
public function calculate_constants($first,$last)
{
if($this->n>1)
{
for($j=0; $j<count($first); $j++)
{
if($first[$j]>=0 && $first[$j]<3)
{
if($last[$j]>=0 && $last[$j]<3)
{
$this->arr.$first[$j][$last[$j]]+=1;
}
}
}
}
else
{
for($k=0; $k<3; $k++)
{
$this->p0.$k=$this->arr0[$k]/($this->arr0[0]+$this->arr0[1]+$this->arr0[2]);
}
for($k=0; $k<3; $k++)
{
$this->p1.$k=$this->arr1[$k]/($this->arr1[0]+$this->arr1[1]+$this->arr1[2]);
}
for($k=0; $k<3; $k++)
{
$this->p2.$k=$this->arr2[$k]/($this->arr2[0]+$this->arr2[1]+$this->arr2[2]);
}
}
$this->n-=1;
}
public function display_constants()
{
echo $this->p00.'\n'.$this->p01.'\n'.$this->p02;
echo $this->p10.'\n'.$this->p11.'\n'.$this->p12;
echo $this->p20.'\n'.$this->p21.'\n'.$this->p22;
}
}
$m= new Markov(5);
$m->calculate_constants(array(0,1,2),array(1,2,1));
$m->display_constants();
?>
在这个我的变量值p00,p01等没有改变,即使我已经在函数中使用它来计算它们的值。请帮我纠正代码,以便获得所需的输出
答案 0 :(得分:1)
尝试在文件顶部添加以下内容(位于<?php
标记下方)
error_reporting(-1);
ini_set('display_errors', true);
空白页通常表示错误,但默认情况下会隐藏这些错误。
答案 1 :(得分:1)
我不知道你真正想要的是什么,但现在你不会有任何错误。一个问题是你没有在数组$ arr0中分配值,但是你正在使用它,我尝试分配for循环来获取错误。
<?php
class Markov {
protected $arr0;
protected $arr1;
protected $arr2;
public $n;
public $p00;
public $p01;
public $p02;
public $p10;
public $p11;
public $p12;
public $p20;
public $p21;
public $p22;
public function __construct($p)
{
$this->arr0=new SplFixedArray(3);
$this->arr1=new SplFixedArray(3);
$this->arr2=new SplFixedArray(3);
$this->n = $p;
for($i=0; $i<3; $i++)
{
$this->arr0[$i]=0;
$this->arr1[$i]=0;
$this->arr2[$i]=0;
}
}
public function calculate_constants($first,$last)
{
if($this->n > 1){
for($j=0; $j<count($first); $j++){
if($first[$j]>=0 && $first[$j]<3){
if($last[$j]>=0 && $last[$j]<3){
$var = 'arr'.$first[$j];
$this->{$var}[$last[$j]] += 1;
}
}
}
} else {
for($k=0; $k<3; $k++) {
$this->{'p0'.$k}=$this->arr0[$k]/($this->arr0[0]+$this->arr0[1]+$this->arr0[2]);
}
for($k=0; $k<3; $k++) {
$this->{'p1'.$k}=$this->arr1[$k]/($this->arr1[0]+$this->arr1[1]+$this->arr1[2]);
}
for($k=0; $k<3; $k++) {
$this->{'p2'.$k}=$this->arr2[$k]/($this->arr2[0]+$this->arr2[1]+$this->arr2[2]);
}
}
$this->n -= 1;
}
public function display_constants() {
echo $this->p00.'\n'.$this->p01.'\n'.$this->p02;
echo $this->p10.'\n'.$this->p11.'\n'.$this->p12;
echo $this->p20.'\n'.$this->p21.'\n'.$this->p22;
}
}
$m= new Markov(5);
$m->calculate_constants(array(0,1,2),array(1,2,1));
$m->display_constants();
?>
由于你的其他条件没有执行,$ p00,$ p01 ....等总是空白。您可以将循环移动到其他功能:
public function display_constants() {
for($k=0; $k<3; $k++) {
$this->{'p0'.$k}=$this->arr0[$k]/($this->arr0[0]+$this->arr0[1]+$this->arr0[2]);
}
for($k=0; $k<3; $k++) {
$this->{'p1'.$k}=$this->arr1[$k]/($this->arr1[0]+$this->arr1[1]+$this->arr1[2]);
}
for($k=0; $k<3; $k++) {
$this->{'p2'.$k}=$this->arr2[$k]/($this->arr2[0]+$this->arr2[1]+$this->arr2[2]);
}
echo $this->p00.'<br>'.$this->p01.'<br>'.$this->p02;
echo $this->p10.'<br>'.$this->p11.'<br>'.$this->p12;
echo $this->p20.'<br>'.$this->p21.'<br>'.$this->p22;
}
答案 2 :(得分:1)
请在文件顶部设置error_reporting(-1);
和ini_set('display_errors', true);
。