0

我对 PHP 完全陌生。我试图制作一个简单的计算脚本。我面临的问题是,当有人输入像“98,7”这样的数字时,数字会四舍五入为“99”,脚本会打印出“99”而不是“100”。所以我的问题是如何处理这个问题。

我的脚本:

                if($omtrek >=53 && $omtrek <=57) $omtrek=55;
                elseif($omtrek >=58 && $omtrek <=62) $omtrek=60;
                elseif($omtrek >=63 && $omtrek <=67) $omtrek=65;
                elseif($omtrek >=68 && $omtrek <=72) $omtrek=70;
                elseif($omtrek >=73 && $omtrek <=77) $omtrek=75;
                elseif($omtrek >=78 && $omtrek <=82) $omtrek=80;
                elseif($omtrek >=83 && $omtrek <=87) $omtrek=85;
                elseif($omtrek >=88 && $omtrek <=92) $omtrek=90;
                elseif($omtrek >=93 && $omtrek <=97) $omtrek=95;
                elseif($omtrek >=98 && $omtrek <=102) $omtrek=100;
                elseif($omtrek >=103 && $omtrek <=107) $omtrek=105;
                elseif($omtrek >=108 && $omtrek <=112) $omtrek=110;
                elseif($omtrek >=113 && $omtrek <=117) $omtrek=115;
                elseif($omtrek >=118 && $omtrek <=122) $omtrek=120;
                elseif($omtrek >=123 && $omtrek <=127) $omtrek=125;
                elseif($omtrek >=128 && $omtrek <=132) $omtrek=130;
                elseif($omtrek >=133 && $omtrek <=137) $omtrek=135;
                elseif($omtrek >=138 && $omtrek <=142) $omtrek=140; 

                if ($omtrek < 53){
                    print 'Helaas'; 
                }
                elseif ($omtrek > 142){
                    print 'XXL'; 
                }
                elseif ($omtrek = round($omtrek)){
                    print("maat: " . $omtrek . "<br/>");
                }
4

4 回答 4

4

You're trying to round to the nearest "5" instead of "1". So, we can use that to greatly simplify what you're doing:

$omtrek = 5 * round($omtrek / 5);

That replaces your gigantic if statement tree. So your entire code boils down to:

if ($omtrek < 53){
    print 'Helaas'; 
} elseif ($omtrek > 142){
    print 'XXL'; 
} else {
    print "maat: " . (5 * round($omtrek / 5)) . "<br/>";
}

Additionally, as for your original point, that should be handled by your if structure, since 98.7 falls within one of the conditions.

于 2012-07-18T16:08:19.287 回答
2

Rather than having a long list of if() statements, why not just apply a little math?

$ cat om.php
#!/usr/local/bin/php
<?php

$omtrek = array( 52, 59, 61, 62, 63, 96.7, 98.7, 150);

foreach ($omtrek as $value) {
  if ($value < 53){
    $output='Helaas'; 
  } elseif ($value > 142){
    $output='XXL'; 
  } else {
    $output = round($value/5)*5;
  }
  printf("%6s => %s\n", $value, $output);
}

$ ./om.php
    52 => Helaas
    59 => 60
    61 => 60
    62 => 60
    63 => 65
  96.7 => 95
  98.7 => 100
   150 => XXL
$ 
于 2012-07-18T16:07:53.663 回答
1

我认为 ghoti 和 ircmaxel 提供的答案比这更好。

然而,因为您是 PHP 新手……您可能不知道如果其中一个if()评估为真,那么“else”链中的其余部分甚至都不会被考虑。所以这会起作用:

            if ($omtrek < 53) $omtrek="Helaas";
            elseif ($omtrek <= 57) $omtrek=55;
            elseif ($omtrek <= 62) $omtrek=60;
            elseif ($omtrek <= 67) $omtrek=65;
            elseif ($omtrek <= 72) $omtrek=70;
            elseif ($omtrek <= 77) $omtrek=75;
            elseif ($omtrek <= 82) $omtrek=80;
            elseif ($omtrek <= 87) $omtrek=85;
            elseif ($omtrek <= 92) $omtrek=90;
            elseif ($omtrek <= 97) $omtrek=95;
            elseif ($omtrek <= 102) $omtrek=100;
            elseif ($omtrek <= 107) $omtrek=105;
            elseif ($omtrek <= 112) $omtrek=110;
            elseif ($omtrek <= 117) $omtrek=115;
            elseif ($omtrek <= 122) $omtrek=120;
            elseif ($omtrek <= 127) $omtrek=125;
            elseif ($omtrek <= 132) $omtrek=130;
            elseif ($omtrek <= 137) $omtrek=135;
            elseif ($omtrek <= 142) $omtrek=140;
            else $omtrek="XXL";

            print "maat: " . $omtrek . "<br/>";
于 2012-07-18T16:22:29.027 回答
1

我认为您正在寻找的功能是ceil

<?php
 $omtrek = 98.7;

 $omtrek=ceil($omtrek);

  if($omtrek >=53 && $omtrek <=57) $omtrek=55;
  elseif($omtrek >=58 && $omtrek <=62) $omtrek=60;
  ....

然后您可以删除圆形功能,如下所示...

if ($omtrek < 53){
    print 'Helaas';
}
elseif ($omtrek > 142){
     print 'XXL';
}
else{
    print("maat: " . $omtrek . "<br/>");

}

于 2012-07-18T00:25:44.510 回答