본문 바로가기

Javascript

제어문 - 선택문 - if, else if, else (+예시 학점 매기기, 월 계절 맞추기)

아래에 코드가 있어요

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3> 제어문 -> 선택문 -> if, else if, else </h3><hr>
    <script>
        gender = "남자";
        //if문 ()안에는 반드시 true, false값이 들어간다.
        if(gender=="남자"){
            document.writeln("당신은 남자입니다.");
        }else{
            document.writeln("당신은 여자입니다.");
        }
        document.writeln("<br>");
        //제어문 전체에 적용 :
        // 실행되는 코드에 {}중괄호가 없으면 밑에 또는 옆에 있는 한줄만 적용.
        //실행되는 코드가 두줄 이상이면 반드시 중괄호 사용.
        if(false)
            document.writeln("실행1");
            document.writeln("실행2"); //if문에 적용안됨.
    
        if(falsedocument.writeln("실행3");
        document.writeln("실행4");
        document.writeln("<br>");
        /////////////////////////////////////////////////////////
        //초등학교 1~2:저학년
        grade = "3";
        if(grade==1||grade==2){
            document.writeln("저학년입니다");
        } else if(grade==3||grade==4){
            document.writeln("중학년입니다.");
        } else if(grade==5||grade==6){
            document.writeln("고학년입니다.");
        } else{
            document.writeln("범위를 벗어났습니다.");
        }
    </script>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

학점 매기기

 

 

 

아래 코드 있어요!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>if-else  학점 매기기</h3><br>
    <script>
        var grade;
        var score = prompt("홍길동님 점수를 입력하세요.",85); //85숫자가 아니라 문자로 알아들음...그래서
        score = parseInt(score);//문자열이 정수로 형변환 해주는 함수
        //위에 함수 설명하려고 코드한줄 -> document.writeln(score+10);
        if(score>=90)
        grade = "A";
        else if(score>=80)
        grade = "B";
        else if(score>70)
        grade = "C";
        else if(score>70)
        grade = "D";
        else
        grade = "F";
        document.writeln(score+"는 "+grade+"입니다.");
 
    </script>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

1월~12월까지 계절 맞추기

 

아래에 코드있어요!

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>1월~12월까지 계절 맞추기</h3><hr>
    <script>
        var month = prompt("당신이 좋아하는 월을 입력하세요",3);
        //12,1,2은 겨울, 3~5:봄, 6~8 여름, 9~11 가을
        //출력결과는 ex)3월은 봄입니다. ex)해당하는 계절이 없습니다.
        //if-else, else 사용 
        var season;
        if(month==3||month==4||month==5)
         season = "봄";
         else if(month==6||month==7||month==8)
         season = "여름";
         else if(month==9||month==10||month==11)
         season = "가을";
         else if(month==12||month==1||month==2)
         season = "겨울";
         else 
            document.writeln("해당하는 계절이 없습니다.");
        document.writeln(month+"월은 "+season+"입니다.");
        </script>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs