我在这里有点问题。我已经检查了很多次错误了,但是所有的我都无法正常工作。因此,我必须创建一个读取3行输入的程序。这是关于一家杂货店,在一周和周末有两种不同的价格值。我必须使其仅输出水果的价格*数量,如果不存在日期或水果,则输出“错误”。我真的很困惑为什么我的代码无法正常工作。我才刚开始使用PHP,所以也许我只是没有太多经验。
$type = strtolower(readline());
$day = strtolower(readline());
$quantity = floatval(readline());
$days = array("monday", "tuesday", "wednesday", "thursday", "friday");
$weekend = array("saturday", "sunday");
$fruit_normal = array(
"2.50" => "banana",
"1.20" => "apple",
"0.85" => "orange",
"1.45" => "grapefruit",
"2.70" => "kiwi",
"5.50" => "pineapple",
"3.85" => "grapes"
);
$fruit_we = array(
"2.70" => "banana",
"1.25" => "apple",
"0.90" => "orange",
"1.60" => "grapefruit",
"3.00" => "kiwi",
"5.60" => "pineapple",
"4.20" => "grapes"
);
$search = array_search($day, $days);
$search1 = array_search($day, $weekend);
$search2 = array_search($type, $fruit_normal);
$search3 = array_search($type, $fruit_we);
if ($search != NULL && $search2 != NULL) {
$price = $search2 * $quantity;
$formatted = number_format($price, 2);
print $formatted;
} elseif ($search1 != NULL && $search3 != NULL) {
$price = $search3 * $quantity;
$formatted = number_format($price, 2);
print $formatted;
} elseif ($search == NULL && $search1 == NULL && $search2 == NULL && $search3 == NULL) {
echo "error";
}
答案 0 :(得分:0)
所以这就是我解决问题的方式,首先我制作了所有仅由参数组成的数组,从1开始。然后在检查{{的结果时,将所有NULL
的值替换为FALSE
1}}我仍然不确定真正的问题是什么,但是现在我会接受的。
array_search