switch 잘 안돼요. 채택완료
score field에 0을 넣거나, 아무것도 안넣었을때 결과가 "you got an A"로 나옵니다. 분명 잘못된게 없는거 같은데.. 절라 간단한건데 뭐가 잘못됐는지 모르겠습니다. 도와 주세요. 당근, 0을 넣거나 아무것도 안 넣었으면 "you got failed"가 나와야 하는데. --switch.htm--
--switch.php-- 70: echo "you got an A"; break; case $score > 60: echo "you got an B"; break; default: echo "you failed"; break; } ?>답변 3개
아래처럼 해보심이.....
<form action="<?=$PHP_SELF?>" method="post">
score:<input type="text" name="score"><input type="submit">
</form>
<?
if($HTTP_POST_VARS["score"] && is_numeric($score)) {
switch ($score){
case $score > 70 :
echo "you got an A";
break;
case $score > 60 :
echo "you got an B";
break;
default :
echo "you failed";
break;
}
} else die("score를 삽입하십시요.");
?>
댓글을 작성하려면 로그인이 필요합니다.
switch 문을 이용해서 greater than 과 less than 을 비교하는건 올바르지 않습니다.
http://us2.php.net/manual/kr/control-structures.switch.php">http://us2.php.net/manual/kr/control-structures.switch.php
if 문으로 처리해주세요.
http://community.sitepoint.com/t/greater-lesser-than-a-numeral-php-switch/3381/3">http://community.sitepoint.com/t/greater-lesser-than-a-numeral-php-switch/3381/3
댓글을 작성하려면 로그인이 필요합니다.
제가 보기에는
switch ($score){
case $score > 70:
echo "you got an A";
break;
case $score > 60:
echo "you got an B";
break;
default:
echo "you failed";
break;
}
이부분이 논리적으로 좀 이상하네요
70보다 크다면 71.....
60보다 크다면 61.....70.....
이렇게 되는데 좀 중복되는 부분이 생기는 것 같은데요
좀 더 다르게 조건을 주시는게 좋지 않을가요??
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인