質問編集履歴
1
ソースコード更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -184,4 +184,164 @@
|
|
184
184
|
|
185
185
|
|
186
186
|
|
187
|
+
### 最新のソースコード
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
```swift
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
import UIKit
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
class ViewController: UIViewController {
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
@IBOutlet weak var ninzu: UITextField!
|
204
|
+
|
205
|
+
@IBOutlet weak var kingaku: UITextField!
|
206
|
+
|
207
|
+
@IBOutlet weak var kekka: UILabel!
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
override func viewDidLoad() {
|
212
|
+
|
187
|
-
|
213
|
+
super.viewDidLoad()
|
214
|
+
|
215
|
+
// Do any additional setup after loading the view, typically from a nib.
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
self.ninzu.keyboardType = UIKeyboardType.numberPad
|
220
|
+
|
221
|
+
self.kingaku.keyboardType = UIKeyboardType.numberPad
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
override func didReceiveMemoryWarning() {
|
228
|
+
|
229
|
+
super.didReceiveMemoryWarning()
|
230
|
+
|
231
|
+
// Dispose of any resources that can be recreated.
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
@IBAction func keisan(_ sender: UIButton) {
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
guard let kingaku = kingaku.text, let ninzu = ninzu.text else { return }
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
if kingaku.isEmpty || ninzu.isEmpty {
|
250
|
+
|
251
|
+
//UIAlertControllerインスタンスの作成
|
252
|
+
|
253
|
+
let alertController = UIAlertController(title: "エラー", message: "合計金額又は、人数が未入力です。", preferredStyle: .alert)
|
254
|
+
|
255
|
+
//追加するアクションの設定
|
256
|
+
|
257
|
+
let ryoukaiAction = UIAlertAction(title: "了解", style: .default, handler: nil)
|
258
|
+
|
259
|
+
//設定したアラートアクションをUIAlertControllerに追加する
|
260
|
+
|
261
|
+
alertController.addAction(ryoukaiAction)
|
262
|
+
|
263
|
+
//アラートを表示
|
264
|
+
|
265
|
+
present(alertController, animated: true, completion: nil)
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
} else {
|
270
|
+
|
271
|
+
guard let kingakuInt = Int(kingaku), let ninzuInt = Int(ninzu) else{
|
272
|
+
|
273
|
+
// Int型に変更できない文字が入力された場合
|
274
|
+
|
275
|
+
return
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
guard kingakuInt > 100, ninzuInt > 1 else{
|
282
|
+
|
283
|
+
// 金額、人数に0以下が入力された場合
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
let alertController = UIAlertController(title: "エラー", message: "数字が無効です", preferredStyle: .alert)
|
288
|
+
|
289
|
+
//追加するアクションの設定
|
290
|
+
|
291
|
+
let ryoukaiAction = UIAlertAction(title: "了解", style: .default, handler: nil)
|
292
|
+
|
293
|
+
//設定したアラートアクションをUIAlertControllerに追加する
|
294
|
+
|
295
|
+
alertController.addAction(ryoukaiAction)
|
296
|
+
|
297
|
+
//アラートを表示
|
298
|
+
|
299
|
+
present(alertController, animated: true, completion: nil)
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
return
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
let intKekka = kingakuInt / ninzuInt
|
312
|
+
|
313
|
+
var result = 0
|
314
|
+
|
315
|
+
if kingakuInt % ninzuInt == 0 {
|
316
|
+
|
317
|
+
result = intKekka
|
318
|
+
|
319
|
+
} else {
|
320
|
+
|
321
|
+
// 100単位で切り上げ
|
322
|
+
|
323
|
+
result = Int(ceil(Double(intKekka) / 100) * 100)
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
print("(result)円")
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
@IBAction func reset(_ sender: UIButton) {
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
ninzu.text = ""
|
340
|
+
|
341
|
+
kingaku.text = ""
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
```
|