回顾:配送规则
Coddy 面向 Ruby 开发者的 PHP 课程的第 12 课,共 48 课。
挑战
简单输入是 [total, member],其中 total 是非负整数,member 是布尔值。total 大于或等于 50 时,shipping 返回 0;或者对于 total 大于或等于 20 的会员,shipping 返回 0;其他情况下返回 5。
提供的主脚本会从标准输入读取一个 JSON 值,检查指定的公共 API,并打印结果。实现模块声明时不要打印内容;不要替换启动脚本,也不要从模块中读取输入。输入符合所述类型和范围。
编辑 Functions.php。main.php 是锁定的入口文件。依赖项使用 require_once __DIR__ 和相对文件名。
亲手试试
<?php
declare(strict_types=1);
require_once __DIR__."/Functions.php";
function ensure(bool $ok,string $message):void{if(!$ok)throw new RuntimeException("API: ".$message);}
$input=json_decode(trim(stream_get_contents(STDIN)),true,512,JSON_THROW_ON_ERROR);
ensure(shipping(0,true)===5 && shipping(30,false)===5,"shipping");
echo (string)shipping(...$input),"\n";
面向 Ruby 开发者的 PHP中的所有课
自己练习: PHP 在线编译器