代码输出“我将要去洛杉矶,旅程将有300英里的距离,我们需要停下2点。”当它应该根据我的IF语句中定义的数字来拉取单词“stop”的复数或单数时(在这种情况下,它应该是复数)。我猜测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();
碰巧有相同答案的不同问题不重复。为了获得一些额外的点而不必要的适度调整会损害Stackoverflow产品。 – Andy