今自作の電卓を作っているのですが
数字を足すところまではできたのですが
+を押して2番目の値を入力することができません
演算子を変えてみたりもしましたが無理でした
+を押すとatai1が一回目は初期化されてまた数値が足されて
二回目は初期化されずに足されていくばかりでした
どのようにしたらいいのか
ご回答お願いします
java
1import java.math.BigInteger; 2 3import javax.swing.JFrame; 4import javax.swing.JPanel; 5import javax.swing.JLabel; 6import javax.swing.JButton; 7 8import java.awt.Color; 9import java.awt.BorderLayout; 10import java.awt.event.ActionListener; 11import java.awt.event.ActionEvent; 12 13public class denntaku extends JFrame implements ActionListener{ 14 15 JPanel Panel; 16 17 JLabel atai1; 18 JLabel atai2; 19 JLabel atai3; 20 21 JButton zero; 22 JButton one; 23 JButton two; 24 JButton three; 25 JButton fore; 26 JButton five; 27 JButton six; 28 JButton seven; 29 JButton eight; 30 JButton nine; 31 32 JButton plus; 33 34 public static final String Cmd_zero = "zero"; 35 public static final String Cmd_one = "one"; 36 public static final String Cmd_two = "two"; 37 public static final String Cmd_three = "three"; 38 public static final String Cmd_fore = "fore"; 39 public static final String Cmd_five = "five"; 40 public static final String Cmd_six = "six"; 41 public static final String Cmd_seven = "seven"; 42 public static final String Cmd_eight = "eight"; 43 public static final String Cmd_nine = "nine"; 44 45 public static final String Cmd_plus = "plus"; 46 47 int count; 48 49 int NN1; 50 int NN2; 51 int NN3; 52 53 String NNN1; 54 String NNN2; 55 String NNN3; 56 57 public static void main(String[] args){ 58 59 denntaku Frame = new denntaku(); 60 Frame.setSize(500, 400); 61 Frame.setTitle("自作電卓"); 62 Frame.setLocationRelativeTo(null); 63 Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 64 65 Frame.setVisible(true); 66 67 } 68 69 denntaku(){ 70 71 count = 0; 72 73 NN1 = 0; 74 NN2 = 0; 75 NN3 = 0; 76 77 NNN1 = Integer.toString(NN1); 78 NNN2 = Integer.toString(NN2); 79 NNN3 = Integer.toString(NN3); 80 81 Panel = new JPanel(); 82 Panel.setLayout(null); 83 84 atai1 = new JLabel(); 85 atai2 = new JLabel(); 86 atai3 = new JLabel(); 87 88 atai1.setText(NNN1); 89 atai2.setText(NNN2); 90 atai3.setText(NNN3); 91 92 atai1.setBounds(10, 10, 100, 20); 93 atai2.setBounds(10, 30, 100, 20); 94 atai3.setBounds(10, 50, 100, 20); 95 96 zero = new JButton("0"); 97 one = new JButton("1"); 98 two = new JButton("2"); 99 three = new JButton("3"); 100 fore = new JButton("4"); 101 five = new JButton("5"); 102 six = new JButton("6"); 103 seven = new JButton("7"); 104 eight = new JButton("8"); 105 nine = new JButton("9"); 106 107 plus = new JButton("+"); 108 109 zero.setBounds(70, 230, 50, 50); 110 one.setBounds(20, 80, 50, 50); 111 two.setBounds(70, 80, 50, 50); 112 three.setBounds(120, 80, 50, 50); 113 fore.setBounds(20, 130, 50, 50); 114 five.setBounds(70, 130, 50, 50); 115 six.setBounds(120, 130, 50, 50); 116 seven.setBounds(20, 180, 50, 50); 117 eight.setBounds(70, 180, 50, 50); 118 nine.setBounds(120, 180, 50, 50); 119 120 plus.setBounds(170, 80, 50, 50); 121 122 zero.setForeground(Color.BLACK); 123 zero.setBackground(Color.WHITE); 124 one.setForeground(Color.BLACK); 125 one.setBackground(Color.WHITE); 126 two.setForeground(Color.BLACK); 127 two.setBackground(Color.WHITE); 128 three.setForeground(Color.BLACK); 129 three.setBackground(Color.WHITE); 130 fore.setForeground(Color.BLACK); 131 fore.setBackground(Color.WHITE); 132 five.setForeground(Color.BLACK); 133 five.setBackground(Color.WHITE); 134 six.setForeground(Color.BLACK); 135 six.setBackground(Color.WHITE); 136 seven.setForeground(Color.BLACK); 137 seven.setBackground(Color.WHITE); 138 eight.setForeground(Color.BLACK); 139 eight.setBackground(Color.WHITE); 140 nine.setForeground(Color.BLACK); 141 nine.setBackground(Color.WHITE); 142 143 plus.setForeground(Color.BLACK); 144 plus.setBackground(Color.WHITE); 145 146 zero.setHorizontalAlignment(JButton.CENTER); 147 zero.setVerticalAlignment(JButton.CENTER); 148 one.setHorizontalAlignment(JButton.CENTER); 149 one.setVerticalAlignment(JButton.CENTER); 150 two.setHorizontalAlignment(JButton.CENTER); 151 two.setVerticalAlignment(JButton.CENTER); 152 three.setHorizontalAlignment(JButton.CENTER); 153 three.setVerticalAlignment(JButton.CENTER); 154 fore.setHorizontalAlignment(JButton.CENTER); 155 fore.setVerticalAlignment(JButton.CENTER); 156 five.setHorizontalAlignment(JButton.CENTER); 157 five.setVerticalAlignment(JButton.CENTER); 158 six.setHorizontalAlignment(JButton.CENTER); 159 six.setVerticalAlignment(JButton.CENTER); 160 seven.setHorizontalAlignment(JButton.CENTER); 161 seven.setVerticalAlignment(JButton.CENTER); 162 eight.setHorizontalAlignment(JButton.CENTER); 163 eight.setVerticalAlignment(JButton.CENTER); 164 nine.setHorizontalAlignment(JButton.CENTER); 165 nine.setVerticalAlignment(JButton.CENTER); 166 167 plus.setHorizontalAlignment(JButton.CENTER); 168 plus.setVerticalAlignment(JButton.CENTER); 169 170 zero.setActionCommand(Cmd_zero); 171 one.setActionCommand(Cmd_one); 172 two.setActionCommand(Cmd_two); 173 three.setActionCommand(Cmd_three); 174 fore.setActionCommand(Cmd_fore); 175 five.setActionCommand(Cmd_five); 176 six.setActionCommand(Cmd_six); 177 seven.setActionCommand(Cmd_seven); 178 eight.setActionCommand(Cmd_eight); 179 nine.setActionCommand(Cmd_nine); 180 181 plus.setActionCommand(Cmd_plus); 182 183 Panel.add(atai1); 184 Panel.add(atai2); 185 Panel.add(atai3); 186 187 Panel.add(zero); 188 Panel.add(one); 189 Panel.add(two); 190 Panel.add(three); 191 Panel.add(fore); 192 Panel.add(five); 193 Panel.add(six); 194 Panel.add(seven); 195 Panel.add(eight); 196 Panel.add(nine); 197 198 Panel.add(plus); 199 200 getContentPane().add(Panel, BorderLayout.CENTER); 201 202 zero.addActionListener(this); 203 one.addActionListener(this); 204 two.addActionListener(this); 205 three.addActionListener(this); 206 fore.addActionListener(this); 207 five.addActionListener(this); 208 six.addActionListener(this); 209 seven.addActionListener(this); 210 eight.addActionListener(this); 211 nine.addActionListener(this); 212 213 plus.addActionListener(this); 214 215 } 216 217 public void actionPerformed(ActionEvent e){ 218 219 String cmd = e.getActionCommand(); 220 221 if(cmd.equals(Cmd_plus)){ 222 223 count = 1; 224 225 } 226 227 if(cmd.equals(Cmd_zero) && count == 0){ 228 229 NN1 += 0; 230 NNN1 = Integer.toString(NN1); 231 atai1.setText(NNN1); 232 233 } 234 235 if(cmd.equals(Cmd_one) && count == 0){ 236 237 NN1 += 1; 238 NNN1 = Integer.toString(NN1); 239 atai1.setText(NNN1); 240 241 } 242 243 if(cmd.equals(Cmd_two) && count == 0){ 244 245 NN1 += 2; 246 NNN1 = Integer.toString(NN1); 247 atai1.setText(NNN1); 248 249 } 250 251 if(cmd.equals(Cmd_three) && count == 0){ 252 253 NN1 += 3; 254 NNN1 = Integer.toString(NN1); 255 atai1.setText(NNN1); 256 257 } 258 259 if(cmd.equals(Cmd_fore) && count == 0){ 260 261 NN1 += 4; 262 NNN1 = Integer.toString(NN1); 263 atai1.setText(NNN1); 264 265 } 266 267 if(cmd.equals(Cmd_five) && count == 0){ 268 269 NN1 += 5; 270 NNN1 = Integer.toString(NN1); 271 atai1.setText(NNN1); 272 273 } 274 275 if(cmd.equals(Cmd_six) && count == 0){ 276 277 NN1 += 6; 278 NNN1 = Integer.toString(NN1); 279 atai1.setText(NNN1); 280 281 } 282 283 if(cmd.equals(Cmd_seven) && count == 0){ 284 285 NN1 += 7; 286 NNN1 = Integer.toString(NN1); 287 atai1.setText(NNN1); 288 289 } 290 291 if(cmd.equals(Cmd_eight) && count == 0){ 292 293 NN1 += 8; 294 NNN1 = Integer.toString(NN1); 295 atai1.setText(NNN1); 296 297 } 298 299 if(cmd.equals(Cmd_nine) && count == 0){ 300 301 NN1 += 9; 302 NNN1 = Integer.toString(NN1); 303 atai1.setText(NNN1); 304 305 } 306 307 308 309 if(cmd.equals(Cmd_zero) && count == 1){ 310 311 NN2 += 0; 312 NNN2 = Integer.toString(NN2); 313 atai1.setText(NNN2); 314 315 } 316 317 if(cmd.equals(Cmd_one) && count == 1){ 318 319 NN2 += 1; 320 NNN2 = Integer.toString(NN2); 321 atai1.setText(NNN2); 322 323 } 324 325 if(cmd.equals(Cmd_two) && count == 1){ 326 327 NN2 += 2; 328 NNN2 = Integer.toString(NN2); 329 atai1.setText(NNN2); 330 331 } 332 333 if(cmd.equals(Cmd_three) && count == 1){ 334 335 NN2 += 3; 336 NNN2 = Integer.toString(NN2); 337 atai1.setText(NNN2); 338 339 } 340 341 if(cmd.equals(Cmd_fore) && count == 1){ 342 343 NN2 += 4; 344 NNN2 = Integer.toString(NN2); 345 atai1.setText(NNN2); 346 347 } 348 349 if(cmd.equals(Cmd_five) && count == 1){ 350 351 NN2 += 5; 352 NNN2 = Integer.toString(NN2); 353 atai1.setText(NNN2); 354 355 } 356 357 if(cmd.equals(Cmd_six) && count == 1){ 358 359 NN2 += 6; 360 NNN2 = Integer.toString(NN2); 361 atai1.setText(NNN2); 362 363 } 364 365 if(cmd.equals(Cmd_seven) && count == 1){ 366 367 NN2 += 7; 368 NNN2 = Integer.toString(NN2); 369 atai1.setText(NNN2); 370 371 } 372 373 if(cmd.equals(Cmd_eight) && count == 1){ 374 375 NN2 += 8; 376 NNN2 = Integer.toString(NN2); 377 atai1.setText(NNN2); 378 379 } 380 381 if(cmd.equals(Cmd_nine) && count == 1){ 382 383 NN2 += 9; 384 NNN2 = Integer.toString(NN2); 385 atai1.setText(NNN2); 386 387 } 388 389 } 390 391}
回答2件
あなたの回答
tips
プレビュー