안슈/4강/try, catch

나이를 입력하고 버튼을 누르면

성인인지 미성년자인지

그리고 짝수인지 홀수인지 출력하기

 

try, catch로

글자를 입력후 버튼누르면 팅기는 오류 방지하기

 

 

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="나이를 입력하세요" />
 
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="출력물" />
 
 
    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
 
 
</LinearLayout>
 
 

 

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.example.myapplication07;
 
 
 
public class MainActivity extends AppCompatActivity {
 
    Button bt;
    TextView tv;
    EditText et;
//선언
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        bt = findViewById(R.id.bt);
        et = findViewById(R.id.et);
        tv = findViewById(R.id.tv);
//변수에 id 입력
 
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//버튼 클릭시 작동할것 설정
 
                try {
//시도해봐라
                    int a = Integer.parseInt(et.getText().toString());
         //숫자타입 변수a에 담아라/ et에서 텍스트를 가져와(문자타입으로)서 숫자타입으로 바꾼 것을
            //et에서 빈칸을 가져와 Integer.parseint 하면 팅김오류발생
           //et에서 글자를 가져와 Integer.parseint 하면 팅김오류발생
 
                    if (a < 20) {
                      //만약 a가 20보다 작으면 실행해라
                        if (a % 2 == 0) {
                        //만약 a나누기 2의 나머지가 0이면 실행해라
                            tv.setText("짝수 청소년");
                            //tv의 텍스트를 정해라 "짝수청소년"으로
                        } else {
                        //아니면
                            tv.setText("홀수 청소년");
                            //tv의 텍스트를 정해라  "홀수 청소년"으로
                        }
                    } else {
                    //아닐경우 실행해라(a가 20보다 작지 않을 경우)
                        if (a % 2 == 0) {
                        //만약 a나누기 2가 0이면
                            tv.setText("짝수 성인");
                            //tv의 텍스트를 정해라 "짝수 성인"으로
                        } else {
                        //아니면
                            tv.setText("홀수 성인");
                            //tv의 텍스트를 정해라 "홀수 성인"으로
                        }
                    }
                } catch (Exception e) {
//예외 일 경우 실행해라
                    Toast.makeText(getApplicationContext(),"숫자만 입력해주세요",Toast.LENGTH_SHORT).show();
                    //토스트 메세지로 "숫자만 입력해주세요"를 짧게 출력해라 
 
                }
 
            }
        });
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

댓글

Designed by JB FACTORY