質問編集履歴
1
エラーメッセージの記載場所が分かりづらかったので編集し直します
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,23 +6,271 @@
|
|
6
6
|
|
7
7
|
四つのボタン(+,-,×,÷)に対して同じアクション設定をに作成しておりできるだけ簡易なコードにするためにtagで管理し、同じメソッドで条件分岐をして作成しています。すべてのメソッド自体は変更せずにDouble型に変換し計算する方法があればご教授頂きたいです。
|
8
8
|
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
##計算アプリ、小数点表示なし
|
14
|
+
|
15
|
+
```swift
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
import UIKit
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
class ViewController: UIViewController {
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
@IBOutlet weak var tfValue01: UITextField!
|
30
|
+
|
31
|
+
@IBOutlet weak var tfValue02: UITextField!
|
32
|
+
|
33
|
+
@IBOutlet weak var lbResult: UILabel!
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
//起動時に行うメソッド
|
38
|
+
|
39
|
+
override func viewDidLoad() {
|
40
|
+
|
41
|
+
super.viewDidLoad()
|
42
|
+
|
43
|
+
// Do any additional setup after loading the view, typically from a nib.
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
//メモリエラー時に行うメソッド
|
50
|
+
|
51
|
+
override func didReceiveMemoryWarning() {
|
52
|
+
|
53
|
+
super.didReceiveMemoryWarning()
|
54
|
+
|
55
|
+
// Dispose of any resources that can be recreated.
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
//キーボードの確定ボタンを押した時に行うメソッド
|
62
|
+
|
63
|
+
@IBAction func pushRetrun(_ sender: Any) {
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
//四則演算キーを押した時のメソッド
|
72
|
+
|
73
|
+
@IBAction func doCalc(_ sender: UIButton) {
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
//テキストフィールドの値を定数を宣言(String型)
|
78
|
+
|
79
|
+
let str01 = tfValue01.text!
|
80
|
+
|
81
|
+
let str02 = tfValue02.text!
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
//String型から整数型に変換
|
88
|
+
|
89
|
+
let num01 = Int(str01)!
|
90
|
+
|
91
|
+
let num02 = Int(str02)!
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
var res = 0
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
//+ボタンを押した時
|
102
|
+
|
103
|
+
if sender.tag == 1{
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
res = num01 + num02
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
//-ボタンを押した時
|
114
|
+
|
115
|
+
else if sender.tag == 2{
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
res = num01 - num02
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
//×ボタンを押した時
|
126
|
+
|
127
|
+
else if sender.tag == 3{
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
res = num01 * num02
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
//÷ボタンを押した時
|
138
|
+
|
139
|
+
else if sender.tag == 4{
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
if num02 != 0 {
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
//num02が0ではない時は計算します
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
res = num01 / num02
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
else{
|
160
|
+
|
161
|
+
//num02が0の時は入力できないと表示
|
162
|
+
|
163
|
+
lbResult.text = "0は入力できません"
|
164
|
+
|
165
|
+
return
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
//整数型から文字列型に変換
|
178
|
+
|
179
|
+
let res2 = String(res)
|
180
|
+
|
181
|
+
lbResult.text = res2
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
//キーボードが動作しないようにする
|
186
|
+
|
187
|
+
tfValue01.resignFirstResponder()
|
188
|
+
|
189
|
+
tfValue02.resignFirstResponder()
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
@IBAction func textClear(_ sender: Any) {
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
//値を空で返す
|
202
|
+
|
203
|
+
tfValue01.text = nil
|
204
|
+
|
205
|
+
tfValue02.text = nil
|
206
|
+
|
207
|
+
lbResult.text = nil
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
//キーボードが動作しないようにする
|
214
|
+
|
215
|
+
tfValue01.resignFirstResponder()
|
216
|
+
|
217
|
+
tfValue02.resignFirstResponder()
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
```
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
###自分が変更してみたのですが
|
236
|
+
|
237
|
+
binaly operater '/' cannot be applied to two 'Float' operands
|
238
|
+
|
239
|
+
と怒られてしまいました。
|
240
|
+
|
241
|
+
|
242
|
+
|
9
243
|
###発生している問題・エラーメッセージ
|
10
244
|
|
11
245
|
|
12
246
|
|
13
247
|
```
|
14
248
|
|
15
|
-
|
249
|
+
binaly operater '/' cannot be applied to two 'Float' operands
|
250
|
+
|
251
|
+
|
16
252
|
|
17
253
|
```
|
18
254
|
|
19
255
|
|
20
256
|
|
21
|
-
###
|
257
|
+
###自分が変更したコード
|
22
258
|
|
23
259
|
```swift
|
24
260
|
|
25
|
-
|
261
|
+
//
|
262
|
+
|
263
|
+
// ViewController.swift
|
264
|
+
|
265
|
+
// myCalc
|
266
|
+
|
267
|
+
//
|
268
|
+
|
269
|
+
// Created by 松井 里衣 on 2016/12/28.
|
270
|
+
|
271
|
+
// Copyright © 2016年 松井 里衣. All rights reserved.
|
272
|
+
|
273
|
+
//
|
26
274
|
|
27
275
|
|
28
276
|
|
@@ -100,6 +348,16 @@
|
|
100
348
|
|
101
349
|
|
102
350
|
|
351
|
+
|
352
|
+
|
353
|
+
//整数型からDouble型に変換
|
354
|
+
|
355
|
+
let num03 = Double(str01)!
|
356
|
+
|
357
|
+
let num04 = Double(str02)!
|
358
|
+
|
359
|
+
|
360
|
+
|
103
361
|
|
104
362
|
|
105
363
|
var res = 0
|
@@ -156,7 +414,7 @@
|
|
156
414
|
|
157
415
|
|
158
416
|
|
159
|
-
res = num0
|
417
|
+
res = num03 / num04
|
160
418
|
|
161
419
|
|
162
420
|
|
@@ -240,256 +498,6 @@
|
|
240
498
|
|
241
499
|
|
242
500
|
|
243
|
-
###自分が変更してみたのですが
|
244
|
-
|
245
|
-
binaly operater '/' cannot be applied to two 'Float' operands
|
246
|
-
|
247
|
-
と怒られてしまいました。
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
```swift
|
252
|
-
|
253
|
-
//
|
254
|
-
|
255
|
-
// ViewController.swift
|
256
|
-
|
257
|
-
// myCalc
|
258
|
-
|
259
|
-
//
|
260
|
-
|
261
|
-
// Created by 松井 里衣 on 2016/12/28.
|
262
|
-
|
263
|
-
// Copyright © 2016年 松井 里衣. All rights reserved.
|
264
|
-
|
265
|
-
//
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
import UIKit
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
class ViewController: UIViewController {
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
@IBOutlet weak var tfValue01: UITextField!
|
278
|
-
|
279
|
-
@IBOutlet weak var tfValue02: UITextField!
|
280
|
-
|
281
|
-
@IBOutlet weak var lbResult: UILabel!
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
//起動時に行うメソッド
|
286
|
-
|
287
|
-
override func viewDidLoad() {
|
288
|
-
|
289
|
-
super.viewDidLoad()
|
290
|
-
|
291
|
-
// Do any additional setup after loading the view, typically from a nib.
|
292
|
-
|
293
|
-
}
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
//メモリエラー時に行うメソッド
|
298
|
-
|
299
|
-
override func didReceiveMemoryWarning() {
|
300
|
-
|
301
|
-
super.didReceiveMemoryWarning()
|
302
|
-
|
303
|
-
// Dispose of any resources that can be recreated.
|
304
|
-
|
305
|
-
}
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
//キーボードの確定ボタンを押した時に行うメソッド
|
310
|
-
|
311
|
-
@IBAction func pushRetrun(_ sender: Any) {
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
}
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
//四則演算キーを押した時のメソッド
|
320
|
-
|
321
|
-
@IBAction func doCalc(_ sender: UIButton) {
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
//テキストフィールドの値を定数を宣言(String型)
|
326
|
-
|
327
|
-
let str01 = tfValue01.text!
|
328
|
-
|
329
|
-
let str02 = tfValue02.text!
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
//String型から整数型に変換
|
336
|
-
|
337
|
-
let num01 = Int(str01)!
|
338
|
-
|
339
|
-
let num02 = Int(str02)!
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
//整数型からDouble型に変換
|
346
|
-
|
347
|
-
let num03 = Double(str01)!
|
348
|
-
|
349
|
-
let num04 = Double(str02)!
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
var res = 0
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
//+ボタンを押した時
|
360
|
-
|
361
|
-
if sender.tag == 1{
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
res = num01 + num02
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
}
|
370
|
-
|
371
|
-
//-ボタンを押した時
|
372
|
-
|
373
|
-
else if sender.tag == 2{
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
res = num01 - num02
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
}
|
382
|
-
|
383
|
-
//×ボタンを押した時
|
384
|
-
|
385
|
-
else if sender.tag == 3{
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
res = num01 * num02
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
}
|
394
|
-
|
395
|
-
//÷ボタンを押した時
|
396
|
-
|
397
|
-
else if sender.tag == 4{
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
if num02 != 0 {
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
//num02が0ではない時は計算します
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
res = num03 / num04
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
}
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
else{
|
418
|
-
|
419
|
-
//num02が0の時は入力できないと表示
|
420
|
-
|
421
|
-
lbResult.text = "0は入力できません"
|
422
|
-
|
423
|
-
return
|
424
|
-
|
425
|
-
}
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
}
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
//整数型から文字列型に変換
|
436
|
-
|
437
|
-
let res2 = String(res)
|
438
|
-
|
439
|
-
lbResult.text = res2
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
//キーボードが動作しないようにする
|
444
|
-
|
445
|
-
tfValue01.resignFirstResponder()
|
446
|
-
|
447
|
-
tfValue02.resignFirstResponder()
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
}
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
@IBAction func textClear(_ sender: Any) {
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
//値を空で返す
|
460
|
-
|
461
|
-
tfValue01.text = nil
|
462
|
-
|
463
|
-
tfValue02.text = nil
|
464
|
-
|
465
|
-
lbResult.text = nil
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
//キーボードが動作しないようにする
|
472
|
-
|
473
|
-
tfValue01.resignFirstResponder()
|
474
|
-
|
475
|
-
tfValue02.resignFirstResponder()
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
}
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
}
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
```
|
490
|
-
|
491
|
-
|
492
|
-
|
493
501
|
###(swift3/Xcode8![![イメージ説明](63fa295ff8f204b9edbe4e249665d8fc.png)](55bcf7a1978326eab1320ae98ffd5526.png))
|
494
502
|
|
495
503
|
もし詳しいコードを見ていただけるのであれば
|