質問編集履歴

2

不要な部分の削除

2021/12/05 10:03

投稿

AmoAmo
AmoAmo

スコア0

test CHANGED
@@ -1 +1 @@
1
- Android Studioで電卓を作りたいのですが意味の分からないエラーメッセージが大量に出てしまいました。助けて下さい。
1
+ Android Studioで電卓を作りたいのですがエラーメッセージが大量に出てしまいました。
test CHANGED
File without changes

1

不要な行番号の削除

2021/12/05 10:03

投稿

AmoAmo
AmoAmo

スコア0

test CHANGED
File without changes
test CHANGED
@@ -194,329 +194,331 @@
194
194
 
195
195
  ```kotlin
196
196
 
197
- 1package com.example.dentaku1
198
-
199
- 2
200
-
201
- 3import androidx.appcompat.app.AppCompatActivity
202
-
203
- 4import android.os.Bundle
204
-
205
- 5import kotlinx.android.synthetic.main.activity_main.*
206
-
207
- 6
208
-
209
- 7class MainActivity : AppCompatActivity() {
210
-
211
- 8
212
-
213
- 9 var nStr : String = ""
214
-
215
- 10 val nList = ArrayList<Double>() // arraylist to store numbers
216
-
217
- 11 val oList = ArrayList<Char>() // arraylist to store operations
218
-
219
- 12
220
-
221
- 13 override fun onCreate(savedInstanceState: Bundle?) {
222
-
223
- 14 super.onCreate(savedInstanceState)
224
-
225
- 15 setContentView(R.layout.activity_main)
226
-
227
- 16
228
-
229
- 17 num0.setOnClickListener {
230
-
231
- 18 formula.text = "${formula.text}0"
232
-
233
- 19 nStr += "0"
234
-
235
- 20 }
236
-
237
- 21 num1.setOnClickListener {
238
-
239
- 22 formula.text = "${formula.text}1"
240
-
241
- 23 nStr += "1"
242
-
243
- 24 }
244
-
245
- 25 num2.setOnClickListener {
246
-
247
- 26 formula.text = "${formula.text}2"
248
-
249
- 27 nStr += "2"
250
-
251
- 28 }
252
-
253
- 29 num3.setOnClickListener {
254
-
255
- 30 formula.text = "${formula.text}3"
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
- 33 num4.setOnClickListener {
262
-
263
- 34 formula.text = "${formula.text}4"
264
-
265
- 35 nStr += "4"
266
-
267
- 36 }
268
-
269
- 37 num5.setOnClickListener {
270
-
271
- 38 formula.text = "${formula.text}5"
272
-
273
- 39 nStr += "5"
274
-
275
- 40 }
276
-
277
- 41 num6.setOnClickListener {
278
-
279
- 42 formula.text = "${formula.text}6"
280
-
281
- 43 nStr += "6"
282
-
283
- 44 }
284
-
285
- 45 num7.setOnClickListener {
286
-
287
- 46 formula.text = "${formula.text}7"
288
-
289
- 47 nStr += "7"
290
-
291
- 48 }
292
-
293
- 49 num8.setOnClickListener {
294
-
295
- 50 formula.text = "${formula.text}8"
296
-
297
- 51 nStr += "8"
298
-
299
- 52 }
300
-
301
- 53 num9.setOnClickListener {
302
-
303
- 54 formula.text = "${formula.text}9"
304
-
305
- 55 nStr += "9"
306
-
307
- 56 }
308
-
309
- 57 point.setOnClickListener {
310
-
311
- 58 formula.text = "${formula.text}."
312
-
313
- 59 nStr += "."
314
-
315
- 60 }
316
-
317
- 61 equal.setOnClickListener {
318
-
319
- 62 formula.text = "${formula.text}="
320
-
321
- 63 addList(nStr)
322
-
323
- 64 var result = calcualte().toString()
324
-
325
- 65 formula.text = result
326
-
327
- 66 nStr = result
328
-
329
- 67 nList.clear()
330
-
331
- 68 oList.clear()
332
-
333
- 69 }
334
-
335
- 70 add.setOnClickListener {
336
-
337
- 71 formula.text = "${formula.text}+"
338
-
339
- 72 addList(nStr,'+')
340
-
341
- 73 nStr = ""
342
-
343
- 74 }
344
-
345
- 75 subtract.setOnClickListener {
346
-
347
- 76 formula.text = "${formula.text}-"
348
-
349
- 77 addList(nStr,'-')
350
-
351
- 78 nStr = ""
352
-
353
- 79 }
354
-
355
- 80 multiply.setOnClickListener {
356
-
357
- 81 formula.text = "${formula.text}*"
358
-
359
- 82 addList(nStr,'*')
360
-
361
- 83 nStr = ""
362
-
363
- 84 }
364
-
365
- 85 divide.setOnClickListener {
366
-
367
- 86 formula.text = "${formula.text}/"
368
-
369
- 87 addList(nStr,'/')
370
-
371
- 88 nStr = ""
372
-
373
- 89 }
374
-
375
- 90 delete.setOnClickListener {
376
-
377
- 91 var formulaStr = formula.text.toString()
378
-
379
- 92 if (!formulaStr.isEmpty()) {
380
-
381
- 93 formula.text = formulaStr.subSequence(0,formulaStr.lastIndex)
382
-
383
- 94 }
384
-
385
- 95 if (!nStr.isEmpty()) {
386
-
387
- 96 nStr = nStr.substring(0, nStr.lastIndex)
388
-
389
- 97 }
390
-
391
- 98 }
392
-
393
- 99 percent.setOnClickListener {
394
-
395
- 100 formula.text = "${formula.text}%"
396
-
397
- 101 }
398
-
399
- 102 sign.setOnClickListener {
400
-
401
- 103
402
-
403
- 104 }
404
-
405
- 105 clear.setOnClickListener {
406
-
407
- 106 formula.text = ""
408
-
409
- 107 nStr = ""
410
-
411
- 108 nList.clear()
412
-
413
- 109 oList.clear()
414
-
415
- 110 }
416
-
417
- 111
418
-
419
- 112 } // end fun onCreate
420
-
421
- 113
422
-
423
- 114 fun addList(str : String, ope : Char) {
424
-
425
- 115 try {
426
-
427
- 116 var num = str.toDouble()
428
-
429
- 117 nList.add(num)
430
-
431
- 118 oList.add(ope)
432
-
433
- 119 }catch(e:Exception){
434
-
435
- 120 formula.text = "Numeric error"
436
-
437
- 121 }
438
-
439
- 122 }
440
-
441
- 123
442
-
443
- 124 fun addList(str : String) {
444
-
445
- 125 try {
446
-
447
- 126 var num = str.toDouble()
448
-
449
- 127 nList.add(num)
450
-
451
- 128 }catch(e:Exception){
452
-
453
- 129 formula.text = "Numeric error"
454
-
455
- 130 }
456
-
457
- 131 }
458
-
459
- 132
460
-
461
- 133 fun calcualte() : Double {
462
-
463
- 134
464
-
465
- 135 var i = 0
466
-
467
- 136 while (i < oList.size) {
468
-
469
- 137 //do multiplication and division first
470
-
471
- 138 if(oList.get(i) == '*' || oList.get(i) == '/') {
472
-
473
- 139 var result = if (oList.get(i) == '*') nList.get(i) * 140nList.get(i+1) else nList.get(i) / nList.get(i+1)
474
-
475
- 141 nList.set(i,result)
476
-
477
- 142 nList.removeAt(i+1)
478
-
479
- 143 oList.removeAt(i)
480
-
481
- 144 i--
482
-
483
- 145 }
484
-
485
- 146 // change subtraction to addition
486
-
487
- 147 else if(oList.get(i) == '-'){
488
-
489
- 148 oList.set(i,'+')
490
-
491
- 149 nList.set(i+1,nList.get(i+1) * -1)
492
-
493
- 150 }
494
-
495
- 151 i++
496
-
497
- 152 }
498
-
499
- 153
500
-
501
- 154 // get sum
502
-
503
- 155 var result = 0.0
504
-
505
- 156 for (i in nList){
506
-
507
- 157 result += i
508
-
509
- 158 }
510
-
511
- 159
512
-
513
- 160 return result
514
-
515
- 161 }// end fun calcualte
516
-
517
- 162
518
-
519
- 163} // end class
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