質問編集履歴
2
不要な部分の削除
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Android Studioで電卓を作りたいのですが
|
1
|
+
Android Studioで電卓を作りたいのですがエラーメッセージが大量に出てしまいました。
|
test
CHANGED
File without changes
|
1
不要な行番号の削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -194,329 +194,331 @@
|
|
194
194
|
|
195
195
|
```kotlin
|
196
196
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
31 nStr += "3"
|
197
|
+
package com.example.dentaku1
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
import androidx.appcompat.app.AppCompatActivity
|
202
|
+
|
203
|
+
import android.os.Bundle
|
204
|
+
|
205
|
+
import kotlinx.android.synthetic.main.activity_main.*
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
class MainActivity : AppCompatActivity() {
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
var nStr : String = ""
|
214
|
+
|
215
|
+
val nList = ArrayList<Double>() // arraylist to store numbers
|
216
|
+
|
217
|
+
val oList = ArrayList<Char>() // arraylist to store operations
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
222
|
+
|
223
|
+
super.onCreate(savedInstanceState)
|
224
|
+
|
225
|
+
setContentView(R.layout.activity_main)
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
num0.setOnClickListener {
|
230
|
+
|
231
|
+
formula.text = "${formula.text}0"
|
232
|
+
|
233
|
+
nStr += "0"
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
num1.setOnClickListener {
|
238
|
+
|
239
|
+
formula.text = "${formula.text}1"
|
240
|
+
|
241
|
+
nStr += "1"
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
num2.setOnClickListener {
|
246
|
+
|
247
|
+
formula.text = "${formula.text}2"
|
248
|
+
|
249
|
+
nStr += "2"
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
num3.setOnClickListener {
|
254
|
+
|
255
|
+
formula.text = "${formula.text}3" nStr += "3"
|
258
256
|
|
259
257
|
32 }
|
260
258
|
|
261
|
-
3
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
1
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
259
|
+
3
|
260
|
+
|
261
|
+
num4.setOnClickListener {
|
262
|
+
|
263
|
+
formula.text = "${formula.text}4"
|
264
|
+
|
265
|
+
nStr += "4"
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
num5.setOnClickListener {
|
270
|
+
|
271
|
+
formula.text = "${formula.text}5"
|
272
|
+
|
273
|
+
nStr += "5"
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
num6.setOnClickListener {
|
278
|
+
|
279
|
+
formula.text = "${formula.text}6"
|
280
|
+
|
281
|
+
nStr += "6"
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
num7.setOnClickListener {
|
286
|
+
|
287
|
+
formula.text = "${formula.text}7"
|
288
|
+
|
289
|
+
nStr += "7"
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
num8.setOnClickListener {
|
294
|
+
|
295
|
+
formula.text = "${formula.text}8"
|
296
|
+
|
297
|
+
nStr += "8"
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
num9.setOnClickListener {
|
302
|
+
|
303
|
+
formula.text = "${formula.text}9"
|
304
|
+
|
305
|
+
nStr += "9"
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
point.setOnClickListener {
|
310
|
+
|
311
|
+
formula.text = "${formula.text}."
|
312
|
+
|
313
|
+
nStr += "."
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
equal.setOnClickListener {
|
318
|
+
|
319
|
+
formula.text = "${formula.text}="
|
320
|
+
|
321
|
+
addList(nStr)
|
322
|
+
|
323
|
+
var result = calcualte().toString()
|
324
|
+
|
325
|
+
formula.text = result
|
326
|
+
|
327
|
+
nStr = result
|
328
|
+
|
329
|
+
nList.clear()
|
330
|
+
|
331
|
+
oList.clear()
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
add.setOnClickListener {
|
336
|
+
|
337
|
+
formula.text = "${formula.text}+"
|
338
|
+
|
339
|
+
addList(nStr,'+')
|
340
|
+
|
341
|
+
nStr = ""
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
subtract.setOnClickListener {
|
346
|
+
|
347
|
+
formula.text = "${formula.text}-"
|
348
|
+
|
349
|
+
addList(nStr,'-')
|
350
|
+
|
351
|
+
nStr = ""
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
multiply.setOnClickListener {
|
356
|
+
|
357
|
+
formula.text = "${formula.text}*"
|
358
|
+
|
359
|
+
addList(nStr,'*')
|
360
|
+
|
361
|
+
nStr = ""
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
divide.setOnClickListener {
|
366
|
+
|
367
|
+
formula.text = "${formula.text}/"
|
368
|
+
|
369
|
+
addList(nStr,'/')
|
370
|
+
|
371
|
+
nStr = ""
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
delete.setOnClickListener {
|
376
|
+
|
377
|
+
var formulaStr = formula.text.toString()
|
378
|
+
|
379
|
+
if (!formulaStr.isEmpty()) {
|
380
|
+
|
381
|
+
formula.text = formulaStr.subSequence(0,formulaStr.lastIndex)
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
if (!nStr.isEmpty()) {
|
386
|
+
|
387
|
+
nStr = nStr.substring(0, nStr.lastIndex)
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
percent.setOnClickListener {
|
394
|
+
|
395
|
+
formula.text = "${formula.text}%"
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
sign.setOnClickListener {
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
clear.setOnClickListener {
|
406
|
+
|
407
|
+
formula.text = ""
|
408
|
+
|
409
|
+
nStr = ""
|
410
|
+
|
411
|
+
nList.clear()
|
412
|
+
|
413
|
+
oList.clear()
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
} // end fun onCreate
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
fun addList(str : String, ope : Char) {
|
424
|
+
|
425
|
+
try {
|
426
|
+
|
427
|
+
var num = str.toDouble()
|
428
|
+
|
429
|
+
nList.add(num)
|
430
|
+
|
431
|
+
oList.add(ope)
|
432
|
+
|
433
|
+
}catch(e:Exception){
|
434
|
+
|
435
|
+
formula.text = "Numeric error"
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
fun addList(str : String) {
|
444
|
+
|
445
|
+
try {
|
446
|
+
|
447
|
+
var num = str.toDouble()
|
448
|
+
|
449
|
+
nList.add(num)
|
450
|
+
|
451
|
+
}catch(e:Exception){
|
452
|
+
|
453
|
+
formula.text = "Numeric error"
|
454
|
+
|
455
|
+
}
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
fun calcualte() : Double {
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
var i = 0
|
466
|
+
|
467
|
+
while (i < oList.size) {
|
468
|
+
|
469
|
+
//do multiplication and division first
|
470
|
+
|
471
|
+
if(oList.get(i) == '*' || oList.get(i) == '/') {
|
472
|
+
|
473
|
+
var result = if (oList.get(i) == '*') nList.get(i) *
|
474
|
+
|
475
|
+
nList.get(i+1) else nList.get(i) / nList.get(i+1)
|
476
|
+
|
477
|
+
nList.set(i,result)
|
478
|
+
|
479
|
+
nList.removeAt(i+1)
|
480
|
+
|
481
|
+
oList.removeAt(i)
|
482
|
+
|
483
|
+
i--
|
484
|
+
|
485
|
+
}
|
486
|
+
|
487
|
+
// change subtraction to addition
|
488
|
+
|
489
|
+
else if(oList.get(i) == '-'){
|
490
|
+
|
491
|
+
oList.set(i,'+')
|
492
|
+
|
493
|
+
nList.set(i+1,nList.get(i+1) * -1)
|
494
|
+
|
495
|
+
}
|
496
|
+
|
497
|
+
i++
|
498
|
+
|
499
|
+
}
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
// get sum
|
504
|
+
|
505
|
+
var result = 0.0
|
506
|
+
|
507
|
+
for (i in nList){
|
508
|
+
|
509
|
+
result += i
|
510
|
+
|
511
|
+
}
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
return result
|
516
|
+
|
517
|
+
}// end fun calcualte
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
} // end class
|
520
522
|
|
521
523
|
```
|
522
524
|
|