代码输出"我将前往洛杉矶.行程将是300英里的距离.我们将需要停止2次." 什么时候它应该根据我的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();
Run Code Online (Sandbox Code Playgroud)
你好像忽略了一个小细节.
if ($this->stops = 1)
Run Code Online (Sandbox Code Playgroud)
应该
if ($this->stops == 1)
Run Code Online (Sandbox Code Playgroud)