问题很简单。我做错了什么,但我找不到解决方案。我错过了什么吗?
function harveyScrabble($mot){
$worth10Points = array("y","Y");
$worth5Points = array("v","V");
$worth3Points = array("h","H");
$worth2Points = array("r","R");
$worth1Point = array("a","A","b","B","c","C","d","D","e","E","f","F","g","G","i",
"I","j","J","k","K","l","L","m","M","n","N","o","O","p","P",
"q","Q","s","S","t","T","u","U","w","W","x","X","z","Z");
$total = 0;
for ($i=0; $i <count($mot) ; $i++) {
$char = $mot{i};
if (in_array($char, $worth10Points)) {
$total += 10;
}
elseif (in_array($char, $worth5Points)) {
$total += 5;
}
elseif (in_array($char, $worth3Points)) {
$total += 3;
}
elseif (in_array($char, $worth2Points)) {
$total += 2;
}
else{
$total += 1;
}
}
return $total;
}
当我尝试使用“Harambe”这个单词调用该函数时,它只给我3分。
答案 0 :(得分:0)
您需要更改
for ($i=0; $i <count($mot) ; $i++) {
到
for ($i=0; $i <strlen($mot) ; $i++) {
答案 1 :(得分:0)
你也可以像这样循环这个词:
foreach(str_split($mot) as $letter){
echo "$letter ";
}
此外,你应该多玩一些阵列。如果您学习如何使用它们,您可以显着缩短代码。
function harveyScrabble($word){
$word = strtolower($word);
$total = 0;
$points = ['y' => 10, 'v' => 5, 'h' => 3, 'r' => 2];
foreach(str_split($word) as $letter){
if(isset($p = $points[$letter])){
$total += $p;
} else {
$total += 1;
}
}
return $points;
}