回答編集履歴

3

バグ修正

2017/12/17 12:05

投稿

tignear
tignear

スコア260

test CHANGED
@@ -28,7 +28,9 @@
28
28
 
29
29
  String str2 = editText2.getText().toString();
30
30
 
31
- BigDecimal a,b;
31
+ BigDecimal a = null;
32
+
33
+ BigDecimal b = null;
32
34
 
33
35
  try{
34
36
 

2

バグ修正

2017/12/17 12:05

投稿

tignear
tignear

スコア260

test CHANGED
@@ -28,23 +28,29 @@
28
28
 
29
29
  String str2 = editText2.getText().toString();
30
30
 
31
+ BigDecimal a,b;
32
+
31
33
  try{
32
34
 
33
- BigDecimal a = new BigDecimal(str1);
35
+ a = new BigDecimal(str1);
34
36
 
35
37
  }catch(NumberFormatException e){
36
38
 
37
39
  Toast.makeText(this, "error", Toast.LENGTH_LONG).show();//もっといいメッセージがあるはず
38
40
 
41
+ return;
42
+
39
43
  }
40
44
 
41
45
  try{
42
46
 
43
- BigDecimal b = new BigDecimal(str2);
47
+ b = new BigDecimal(str2);
44
48
 
45
49
  }catch(NumberFormatException e){
46
50
 
47
51
  Toast.makeText(this, "error", Toast.LENGTH_LONG).show();//こっちも
52
+
53
+ return;
48
54
 
49
55
  }
50
56
 

1

コード追加

2017/12/17 12:01

投稿

tignear
tignear

スコア260

test CHANGED
@@ -5,3 +5,75 @@
5
5
  (str2もですが)
6
6
 
7
7
  なので処理できない値がユーザーによって入力されても大丈夫なように例外処理するべきです
8
+
9
+ ```java
10
+
11
+ @Override public void onClick(View v) {
12
+
13
+ /*Intent intent = new Intent(this, SecondActivity.class);
14
+
15
+ intent.putExtra("VALUE1", 10.5);
16
+
17
+ intent.putExtra("VALUE2", 2.5);
18
+
19
+ startActivity(intent);*/
20
+
21
+ EditText editText1 = (EditText)findViewById(R.id.editText1);
22
+
23
+ EditText editText2 = (EditText)findViewById(R.id.editText2);
24
+
25
+ TextView textView1 = (TextView)findViewById(R.id.textView1);
26
+
27
+ String str1 = editText1.getText().toString();
28
+
29
+ String str2 = editText2.getText().toString();
30
+
31
+ try{
32
+
33
+ BigDecimal a = new BigDecimal(str1);
34
+
35
+ }catch(NumberFormatException e){
36
+
37
+ Toast.makeText(this, "error", Toast.LENGTH_LONG).show();//もっといいメッセージがあるはず
38
+
39
+ }
40
+
41
+ try{
42
+
43
+ BigDecimal b = new BigDecimal(str2);
44
+
45
+ }catch(NumberFormatException e){
46
+
47
+ Toast.makeText(this, "error", Toast.LENGTH_LONG).show();//こっちも
48
+
49
+ }
50
+
51
+ if(v.getId() == R.id.button1){
52
+
53
+ BigDecimal c = a.add(b);
54
+
55
+ textView1.setText(String.valueOf(c));
56
+
57
+ }else if(v.getId() == R.id.button2){
58
+
59
+ BigDecimal c = a.subtract(b);
60
+
61
+ textView1.setText(String.valueOf(c));
62
+
63
+ }else if(v.getId() == R.id.button3){
64
+
65
+ BigDecimal c = a.multiply(b);
66
+
67
+ textView1.setText(String.valueOf(c));
68
+
69
+ }else if(v.getId() == R.id.button4){
70
+
71
+ BigDecimal c = a.divide(b,2, BigDecimal.ROUND_HALF_UP);
72
+
73
+ textView1.setText(String.valueOf(c));
74
+
75
+ }
76
+
77
+ }
78
+
79
+ ```