【问题标题】:How can I make an ATM machine with PHP by using only one loop and an array? [closed]如何只使用一个循环和一个数组,用 PHP 制作一台 ATM 机? [关闭]
【发布时间】:2019-02-28 14:03:14
【问题描述】:

我用 PHP 制作了一台 ATM 机,但我使用了 4 个 for 循环。我只想使用一个循环和一个数组。这是我的代码:

private function Atm() {
    $nAmount = $_POST['txtATM'];

    $nFifty = 0;
    for ($V=0; $V < $nAmount; $V+=50) { 
        $nFifty++;
    }
        if ($V > $nAmount) {
            $V-=50;
            $nFifty-=1;
        }

    $nTwenty = 0;
    for ($T=$V; $T < $nAmount; $T+=20) { 
        $nTwenty++;
    }
        if ($T > $nAmount) {
            $T-=20;
            $nTwenty-=1;
        }

    $nTen = 0;
    for ($t=$T; $t < $nAmount; $t+=10) { 
        $nTen++;
    }
        if ($t > $nAmount) {
            $t -= 10;
            $nTen-=1;
        }

    $nFive = 0;
    for ($v=$t; $v < $nAmount; $v+=5) { 
        $nFive++;
    }
        if ($v > $nAmount) {
            $v-=5;
            $nFive-=1;
        }

    echo "You will get: ";
    echo "$nFifty times fifty, $nTwenty times twenty, $nTen times ten and $nFive times five. ";
}

谁能帮我找到一个解决方案,用一个循环和一个数组重写这段代码?

【问题讨论】:

  • 这不是 SO 的工作方式。请使用tour 并阅读How to Ask 指南。
  • 和是 11 发生了什么? (不除以 5?)
  • 私有函数 check() { if (isset($_POST['btnATM'])) { $nAmount = $_POST['txtATM']; if ($nAmount 500) { echo "limit is 500"; }else { 返回 $this->Atm(); } } }

标签: php arrays loops for-loop


【解决方案1】:

你可以用 1 个循环来做到这一点(在这种情况下我更喜欢 while):

$arr = array(50, 20, 10, 5);
$name = array("fifty", "twenty", "ten", "five");
$v = 105;
while ($v && count($arr)) {
    $currentBill = array_shift($arr);
    $change[] = intval($v / $currentBill);
    $v = $v % $currentBill;
}
for ($i = 0; $i < count($change); $i++)
    echo $change[$i] . " of ". $name[$i] .", ";

我建议在开头添加错误检查,以防止不能被 5 整除,依此类推...

【讨论】:

    【解决方案2】:

    我会计算模数以检查有效面额,然后在循环中重复进行整数除法设置字符串数组n次面额,然后加入逗号分隔,除了最后一个以'and'分隔。

    模运算结果是整数除法的余数。如果最小面额有余数,则无法支付。

    整数除法为您提供特定面额的计数。我们从最伟大的开始。余款以较小的面额进行处理。

    function Atm(int $amount)
    {
      $denominations = [50 => 'fifty', 20 => 'twenty', 10 => 'ten' , 5 => 'five'];
    
      if($amount <= 0 || $amount % array_key_last($denominations))
        return 'Amount cannot be paid. $5 is the smallest denomination.' . PHP_EOL;
    
      $arr = [];
    
      foreach($denominations as $denomination => $name)
      {
        $amount -= $denomination * $count = (int) ($amount / $denomination);
        if($count)
          $arr[] = "$count times $name";
      }
    
      return 'You will get: '
           . (1 === count($arr) ? $arr[0] : implode(', ', array_splice($arr, 0, -1)) . ' and ' . $arr[0])
           . '.' . PHP_EOL;
    }
    
    var_dump(Atm(5));
    var_dump(Atm(105));
    var_dump(Atm(115));
    var_dump(Atm(116));
    

    输出:

    string(28) "You will get: 1 times five.
    "
    string(46) "You will get: 2 times fifty and 1 times five.
    "
    string(59) "You will get: 2 times fifty, 1 times ten and 1 times five.
    "
    string(52) "Amount cannot be paid. $5 is the smallest denomination.
    "
    

    TODO:您应该编写一个例程来输出 'once'、'twice'、'n time' 或找到另一个表达式,例如 '1 bill'、'2 bills'。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      相关资源
      最近更新 更多