【发布时间】:2017-03-18 07:06:04
【问题描述】:
我正在使用 CI 框架 PHP,
我想根据四个用户的输入决策做出最终决策结果,
$this->input->post('decision_1', TRUE) == 0 //or 1 or 2 or 3
$this->input->post('decision_2', TRUE) == 0 //or 1 or 2 or 3
$this->input->post('decision_3', TRUE) == 0 //or 1 or 2 or 3
$this->input->post('decision_4', TRUE) == 0 //or 1 or 2 or 3
现在如果任何三个输入相同,那么它将成为最终决策,或者在四个或三个输入中,最大的决策将成为最终决策,
例如,如果决策 1,2,3 为 0,则 final 应为 0 但是如果决策 1,2 是 0,决策 4 是 1,那么 final 也应该是 1。
我尝试使用 switch 语句,但如果所有最少 3 个输入都相同,它确实有效,但如果三个输入不同,则我不起作用,
$result_case = TRUE;
switch ($result_case) {
case $this->input->post('decision_1', TRUE) == 1 && $this->input->post('decision_2', TRUE) == 1 && $this->input->post('decision_3', TRUE) == 1 :
$result = 1;
break;
case $this->input->post('decision_1', TRUE) == 1 && $this->input->post('decision_2', TRUE) == 1 && $this->input->post('decision_4', TRUE) == 1 :
$result = 1;
break;
case $this->input->post('decision_1', TRUE) == 1 && $this->input->post('decision_3', TRUE) == 1 && $this->input->post('decision_4', TRUE) == 1:
$result = 1;
break;
case $this->input->post('decision_2', TRUE) == 1 && $this->input->post('decision_3', TRUE) == 1 && $this->input->post('decision_4', TRUE) == 1:
$result = 1;
break;
//
case $this->input->post('decision_1', TRUE) == 0 && $this->input->post('decision_2', TRUE) == 0 && $this->input->post('decision_3', TRUE) == 0:
$result = 0;
break;
case $this->input->post('decision_1', TRUE) == 0 && $this->input->post('decision_2', TRUE) == 0 && $this->input->post('decision_4', TRUE) == 0:
$result = 0;
break;
case $this->input->post('decision_1', TRUE) == 0 && $this->input->post('decision_3', TRUE) == 0 && $this->input->post('decision_4', TRUE) == 0:
$result = 0;
break;
case $this->input->post('decision_2', TRUE) == 0 && $this->input->post('decision_3', TRUE) == 0 && $this->input->post('decision_4', TRUE) == 0:
$result = 0;
break;
//
case $this->input->post('decision_1', TRUE) == 2 && $this->input->post('decision_2', TRUE) == 2 && $this->input->post('decision_3', TRUE) == 2:
$result = 2;
break;
case $this->input->post('decision_1', TRUE) == 2 && $this->input->post('decision_2', TRUE) == 2 && $this->input->post('decision_4', TRUE) == 2:
$result = 2;
break;
case $this->input->post('decision_1', TRUE) == 2 && $this->input->post('decision_3', TRUE) == 2 && $this->input->post('decision_4', TRUE) == 2:
$result = 2;
break;
case $this->input->post('decision_2', TRUE) == 2 && $this->input->post('decision_3', TRUE) == 2 && $this->input->post('decision_4', TRUE) == 2:
$result = 2;
break;
default:
$result = 'Undecided';
}
是否有任何简单的方法来检查输入决定并做出最终决定?
谢谢,
【问题讨论】:
标签: php codeigniter switch-statement