두 EditText에 있는 값을 아래의 5개 버튼을 누르면 계산결과가 TextView에 나오는 프로그램

실수 끼리 연산했을 경우 결과는 실수, 정수끼리 연산했을 경우 정수로 처리함.
문자가 들어가있을 경우, 0으로 나눌경우 예외처리가 되있음


MainActivity.class

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.calculater;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
    EditText edt1, edt2;
    Button btAdd, btSub, btMul, btDiv,btNa;
    TextView tvOutput;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edt1 = (EditText) findViewById(R.id.edt1);
        edt2 = (EditText) findViewById(R.id.edt2);
        btAdd = (Button) findViewById(R.id.btAdd);
        btSub = (Button) findViewById(R.id.btSub);
        btMul = (Button) findViewById(R.id.btMul);
        btDiv = (Button) findViewById(R.id.btDiv);
        btNa=(Button) findViewById(R.id.btNa);
 
        tvOutput = (TextView) findViewById(R.id.tvOutput);
        btAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double iTemp1 = 0, iTemp2 = 0;
                try {
                    iTemp1 = Double.valueOf(edt1.getText().toString());
                    iTemp2 = Double.valueOf(edt2.getText().toString());
                    double result = iTemp1 + iTemp2;
                    if(result == (int)result) {
                        int iChange = (int) result;
                        tvOutput.setText("계산 결과 : "+String.valueOf(iChange));
                    }
                    else
                    {
                        tvOutput.setText("계산 결과 : "+String.valueOf(result));
                    }
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "입력에 문자가 있습니다.", Toast.LENGTH_SHORT).show();
                }
 
            }
        });
        btSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double iTemp1 = 0, iTemp2 = 0;
                try {
                    iTemp1 = Double.valueOf(edt1.getText().toString());
                    iTemp2 = Double.valueOf(edt2.getText().toString());
                    double result = iTemp1 - iTemp2;
                    if(result == (int)result) {
                        int iChange = (int) result;
                        tvOutput.setText("계산 결과 : "+String.valueOf(iChange));
                    }
                    else
                    {
                        tvOutput.setText("계산 결과 : "+String.valueOf(result));
                    }
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "입력에 문자가 있습니다.", Toast.LENGTH_SHORT).show();
                }
 
            }
        });
        btMul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double iTemp1 = 0, iTemp2 = 0;
                try {
                    iTemp1 = Double.valueOf(edt1.getText().toString());
                    iTemp2 = Double.valueOf(edt2.getText().toString());
                    double result = iTemp1 * iTemp2;
                    if(result == (int)result) {
                        int iChange = (int) result;
                        tvOutput.setText("계산 결과 : "+String.valueOf(iChange));
                    }
                    else
                    {
                        tvOutput.setText("계산 결과 : "+String.valueOf(result));
                    }
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "입력에 문자가 있습니다.", Toast.LENGTH_SHORT).show();
                }
            }
 
        });
        btDiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double iTemp1 = 0, iTemp2 = 0;
                try {
                    iTemp1 = Double.valueOf(edt1.getText().toString());
                    iTemp2 = Double.valueOf(edt2.getText().toString());
                    double result = iTemp1 / iTemp2;
                    if(result == (int)result) {
                        int iChange = (int) result;
                        tvOutput.setText("계산 결과 : "+String.valueOf(iChange));
                    }
                    else
                    {
                        tvOutput.setText("계산 결과 : "+String.valueOf(result));
                    }
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "입력에 문자가 있거나 0으로 나눌수 없습니다", Toast.LENGTH_SHORT).show();
                }
 
            }
 
        });
        btNa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int iTemp1 = 0, iTemp2 = 0;
                try {
                    iTemp1 = Integer.parseInt(edt1.getText().toString());
                    iTemp2 = Integer.parseInt(edt2.getText().toString());
                    int result = iTemp1 % iTemp2;
                    tvOutput.setText(String.valueOf(result));
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "입력에 문자가 있습니다.", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
 
}
cs


activity_main.xml

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
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical">
 
    <EditText
        android:id="@+id/edt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"/>
    <EditText
        android:id="@+id/edt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"/>
    <Button
        android:id="@+id/btAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="더하기"/>
    <Button
        android:id="@+id/btSub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="빼기"/>
    <Button
        android:id="@+id/btMul"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="곱하기"/>
    <Button
        android:id="@+id/btDiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="나누기"/>
    <Button
        android:id="@+id/btNa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="나머지 값"/>
    <TextView android:id="@+id/tvOutput"
        android:text="계산결과"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</LinearLayout>
cs


Calculater 2.zip


+ Recent posts