前提・実現したいこと
kotlinで電卓を作りたいです。
Intを二つ用意して四則演算しているのですが、それぞれ一桁に足し合わされて演算されています。
発生している問題・エラーメッセージ
計算すると0が除外されます。
それと数値が一桁になり演算されます
例) 40 + 5 = 9
7 + 20 = 9
5 * 10 = 5
5 + 12 = 15
71 * 11111 = 40
-> (7 + 1) * (1 + 1 + 1 + 1 + 1) = 40 になっています。
該当のソースコード
kotlin
1package com.example.kdentaku01 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import kotlinx.android.synthetic.main.activity_main.* 6 7var nInt : Int = 0 8var mInt : Int = 0 9var sStr : String = "f" // ope判別 10 11class MainActivity : AppCompatActivity() { 12 13 override fun onCreate(savedInstanceState: Bundle?) { 14 super.onCreate(savedInstanceState) 15 setContentView(R.layout.activity_main) 16 17 18 num9.setOnClickListener { 19 if (sStr.equals("f")) { 20 formula.text = "${formula.text}9" 21 nInt += 9 22 } else { 23 formula.text = "${formula.text}9" 24 mInt += 9 25 } 26 } 27 num8.setOnClickListener { 28 if (sStr.equals("f")) { 29 formula.text = "${formula.text}8" 30 nInt += 8 31 } else { 32 formula.text = "${formula.text}8" 33 mInt += 8 34 } 35 } 36 num7.setOnClickListener { 37 if (sStr.equals("f")) { 38 formula.text = "${formula.text}7" 39 nInt += 7 40 } else { 41 formula.text = "${formula.text}7" 42 mInt += 7 43 } 44 } 45 num6.setOnClickListener { 46 if (sStr.equals("f")) { 47 formula.text = "${formula.text}6" 48 nInt += 6 49 } else { 50 formula.text = "${formula.text}6" 51 mInt += 6 52 } 53 } 54 num5.setOnClickListener { 55 if (sStr.equals("f")) { 56 formula.text = "${formula.text}5" 57 nInt += 5 58 } else { 59 formula.text = "${formula.text}5" 60 mInt += 5 61 } 62 } 63 num4.setOnClickListener { 64 if (sStr.equals("f")) { 65 formula.text = "${formula.text}4" 66 nInt += 4 67 } else { 68 formula.text = "${formula.text}4" 69 mInt += 4 70 } 71 } 72 num3.setOnClickListener { 73 if (sStr.equals("f")) { 74 formula.text = "${formula.text}3" 75 nInt += 3 76 } else { 77 formula.text = "${formula.text}3" 78 mInt += 3 79 } 80 } 81 num2.setOnClickListener { 82 if (sStr.equals("f")) { 83 formula.text = "${formula.text}2" 84 nInt += 2 85 } else { 86 formula.text = "${formula.text}2" 87 mInt += 2 88 } 89 } 90 num1.setOnClickListener { 91 if (sStr.equals("f")) { 92 formula.text = "${formula.text}1" 93 nInt += 1 94 } else { 95 formula.text = "${formula.text}1" 96 mInt += 1 97 } 98 } 99 num0.setOnClickListener { 100 if (sStr.equals("f")) { 101 formula.text = "${formula.text}0" 102 nInt += 0 103 } else { 104 formula.text = "${formula.text}0" 105 mInt += 0 106 } 107 } 108 109 110 111 112 tasu.setOnClickListener { 113 if (sStr.equals("f") && !formula.text.equals("")) { 114 ope.text = "+" 115 formula.text = "" 116 sStr = "s" 117 } 118 } 119 hiku.setOnClickListener { 120 if (sStr.equals("f") && !formula.text.equals("")) { 121 ope.text = "-" 122 formula.text = "" 123 sStr = "s" 124 } 125 } 126 kake.setOnClickListener { 127 if (sStr.equals("f") && !formula.text.equals("")) { 128 ope.text = "x" 129 formula.text = "" 130 sStr = "s" 131 } 132 } 133 waru.setOnClickListener { 134 if (sStr.equals("f") && !formula.text.equals("")) { 135 ope.text = "/" 136 formula.text = "" 137 sStr = "s" 138 } 139 } 140 141 clear.setOnClickListener { 142 formula.text = "" 143 nInt = 0 144 mInt = 0 145 sStr = "f" 146 ope.text = "" 147 } 148 149 150 equal.setOnClickListener { 151 if (sStr.equals("s") && !formula.text.equals("")) { 152 153 when (ope.text) { 154 "+" -> { 155 formula.text = (nInt + mInt).toString() 156 } 157 "-" -> { 158 formula.text = (nInt - mInt).toString() 159 } 160 "x" -> { 161 formula.text = (nInt * mInt).toString() 162 } 163 "/" -> { 164 formula.text = (nInt / mInt).toString() 165 } 166 } 167 168 nInt = 0 169 mInt = 0 170 171 sStr = "f" 172 ope.text = "" 173 } 174 } 175 176 177 } 178 179}
試したこと
IntをDoubleにしてみても変わりませんでした。
補足情報(FW/ツールのバージョンなど)
andoid studio 3.4.2
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/22 02:09
2019/08/22 02:29