2017-02-19 13 views
0

コードは「私はロサンゼルスに行く予定です。旅は300マイルの距離になります。私のIF文で定義されている数に応じて、複数の単語または単数の単語を「停止」する必要がある場合(その場合は、複数形にする必要があります)。私はクラスを構築した方法のためにIFが期待通りに動作しないと思います。それを修正するどんな助けもありがたいです。IFのステートメントが正しく解決されない

class trip { 

    public $destination; 
    public $distance; 
    public $unit; 
    public $stops = null; 
    public $transport = null; 

    function __construct($destination, $distance, $unit, $stops, $transport) { 
    $this->destination = $destination; 
    $this->distance = $distance; 
    $this->unit = $unit; 
    $this->stops = $stops; 
    $this->transport = $transport; 
    } 

    function getTrip() { 
    $a = "I will be going to " . $this->destination . ". "; 
    $a .= "The journey will be a distance of " . $this->distance . " " . $this->unit . ". "; 
    $a .= "We will need to make " . $this->stops; 
    if ($this->stops = 1) { 
     $a .= " stop."; 
    } else { 
     $a .= " stops."; 
    } 
    $a .= "\n"; 
    echo $a; 
    } 
} 

$first = new trip('Los Angeles',300,'miles',2,'airplane'); 
$first->getTrip(); 
+0

同じ回答が発生する異なる質問は重複しません。余分なポイントをいくつか得点するために必要な節度は、Stackoverflow製品を傷つけることです。 – Andy

答えて

5

あなたは細部を見落としたようです。

if ($this->stops = 1) 

if ($this->stops == 1) 
+0

D'oh、それは私の部分の愚かな間違いです...ありがとう。 ;) – Andy

2

あなたのIF文が間違っているようでなければなりません。 、あなたが比較を行うために2つの==兆候を使用する必要が

if($this->stops == 1){ ...

それ以外の割り当てです: 実際には、それがなければなりません。したがって、あなたのケースでは、1つの記号は実際にあなたが期待する状態をテストしません。

関連する問題