首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于四个输入决策进行最终决策

基于四个输入决策进行最终决策
EN

Stack Overflow用户
提问于 2017-03-18 07:06:04
回答 1查看 45关注 0票数 1

我正在使用CI框架PHP,

我想根据四个用户的输入决定做出最终的决定,

代码语言:javascript
复制
$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个输入都是相同的,但是如果三个是不同的,那么我就不工作了,

代码语言:javascript
复制
$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';
  }

有什么简单的方法来检查输入决策并做出最终的决定吗?

谢谢,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-18 07:28:37

代码语言:javascript
复制
//save each post in as a key of an array.
$results[$this->input->post('decision_1', TRUE)][] = 1;
...
...
...
//count each type of the post
$results = array_map(function($v){return count($v);}, $results;);
$max = -1;
foreach($results as $k => $v)
{
  if($v >= 3)
    $result = $k;
  $max = $results[$max] > $v ? $max : $k;

}
//if a post type has more or equal to 3, chose it. otherwise chose the biggest key as the result.
$result = isset($result) ? $result : $max;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42871561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档