質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
if

if文とは様々なプログラミング言語で使用される制御構文の一種であり、条件によって処理の流れを制御します。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

2回答

510閲覧

if文の分岐がうまくいかない

Alpa

総合スコア80

if

if文とは様々なプログラミング言語で使用される制御構文の一種であり、条件によって処理の流れを制御します。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2017/10/18 13:08

今自作の電卓を作っているのですが
数字を足すところまではできたのですが
+を押して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}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

LouiS0616

2017/10/18 13:22

コードを見ると何度も何度も同じような処理を書いているようですが... このようなときこそ、クラスの有用性を知るよい機会だと思うのですが、そちらの勉強はまだしないのですか?
Alpa

2017/10/18 13:24

今勉強中なのですがまだよく理解できなくて使ってないです すいません
LouiS0616

2017/10/18 13:28

うーん... それだったら、とりあえず配列やリストを使うだけでもコード量が激減すると思うのですが...
Alpa

2017/10/18 13:33

頑張ってもう一度考え直してみます
guest

回答2

0

直接的な原因

不具合の直接的な原因は、countの値が0のときと1のときの処理がほぼ同じだからです。

Java

1if(cmd.equals(Cmd_zero) && count == 0){ 2 NN1 += 0; 3 NNN1 = Integer.toString(NN1); 4 atai1.setText(NNN1); 5} 6 7... 8 9if(cmd.equals(Cmd_zero) && count == 1){ 10 NN2 += 0; 11 NNN2 = Integer.toString(NN2); 12 atai1.setText(NNN2); 13}

↑ どちらも結局atai1ラベルにテキストを設定している。

潜在的な原因

似たコードを何度も書いて、保守拡張性が著しく低下しているためです。

  • ボタンzero, one, two...

配列やリストを使わないのでしょうか?
そもそもひな型が全く同じなのであればクラスを定義すべきです。

  • atai1, atai2...

変数名からラベルと言う役割がほとんど想像できません。

  • フラグとして用いられるcount

スコープの広いフラグ変数は避けるべきです。じきに管理しきれなくなります。

  • 延々と続くsetBound

GridLayoutなど、便利なレイアウト機能が色々ありますよ。

追記

突き放すだけ、というのも申し訳ないので、簡単なサンプルを組みました。
詳しくは後述しますが、ディテールはわざとスカスカにしてあります。
実行例
Calculator.java

Java

1package calculator; 2 3import javax.swing.*; 4 5public class Calculator extends JFrame { 6 private Calculator(String title) { 7 super(title); 8 9 Model model = new Model(); 10 add(new GUI(model)); 11 12 pack(); 13 setResizable(false); 14 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 15 } 16 17 public static void main(String[] args){ 18 Calculator calc = new Calculator("My calculator"); 19 calc.setLocationRelativeTo(null); 20 calc.setVisible(true); 21 } 22}

GUI.java

Java

1package calculator; 2 3import javax.swing.*; 4import java.awt.*; 5import java.util.Arrays; 6import java.util.List; 7 8class GUI extends JPanel { 9 private Model model; 10 private JLabel displayLabel; 11 12 GUI(Model model) { 13 this.model = model; 14 model.registerGUI(this); 15 16 displayLabel = new JLabel("0", JLabel.RIGHT); 17 displayLabel.setFont( 18 new Font("Arial", Font.PLAIN, 64) 19 ); 20 displayLabel.setBackground(Color.LIGHT_GRAY); 21 22 setLayout(new BorderLayout()); 23 add(displayLabel, BorderLayout.NORTH); 24 add(new ButtonPanel(), BorderLayout.SOUTH); 25 } 26 27 int getValue() { 28 return Integer.valueOf(displayLabel.getText()); 29 } 30 void setValue(int value) { 31 displayLabel.setText(String.valueOf(value)); 32 } 33 34 private class ButtonPanel extends JPanel { 35 ButtonPanel() { 36 setLayout(new GridLayout(4, 4)); 37 38 for(String elem: keyList){ 39 add(new MyButton(elem, model)); 40 } 41 } 42 43 private List<String> keyList = Arrays.asList( 44 "1", "2", "3", "+", 45 "4", "5", "6", "-", 46 "7", "8", "9", "*", 47 "C", "0", "=", "/" 48 ); 49 } 50 51 class MyButton extends JButton { 52 MyButton(String text, Model model) { 53 super(text); 54 setFont( 55 new Font("Arial", Font.PLAIN, 32) 56 ); 57 58 setForeground(Color.BLACK); 59 setBackground(Color.WHITE); 60 61 setHorizontalAlignment(JButton.CENTER); 62 setVerticalAlignment(JButton.CENTER); 63 64 setPreferredSize( 65 new Dimension(128, 64) 66 ); 67 68 addActionListener( 69 evt -> model.operate(text) 70 ); 71 } 72 } 73}

Model.java

Java

1package calculator; 2 3class Model { 4 private GUI gui; 5 6 void registerGUI(GUI gui) { 7 this.gui = gui; 8 } 9 10 void operate(String text) { 11 int nowValue = gui.getValue(); 12 String nowText = nowValue == 0 ? "" : String.valueOf(nowValue); 13 14 switch(text) { 15 case "C": 16 nowText = "0"; 17 break; 18 19 case "+": case "-": case "*": case "/": 20 break; 21 22 case "=": 23 break; 24 25 // number 26 default: 27 if (nowText.length() > 8) break; 28 nowText += text; 29 break; 30 } 31 32 gui.setValue(Integer.valueOf(nowText)); 33 } 34}

欠陥のある点
実際に動作するのは値の入力とクリアだけで、後は未実装
整数以外が登場する演算に対応できない設計
...どうしてそうなのかはちょっと考えればわかります。
掛け算を実装してもし実行すると、桁あふれが起きる恐れがある

拡張できそうな点
単項演算の追加
...lnとかsinとかexpとかabsとかを足してみるとよいでしょう。
対応桁数の増加
入力された演算子の表示


新しい何かを勉強する際に重要なことは、他の人の書いたコードをよく読むことです。
最初は見よう見まねでも、必死に理解/模倣をするうちに、少しずつわかるものもあります。
また、基礎的な部分はよく学び、積極的に応用するようにしましょう。

ただ、私自身日頃からSwingを使っているわけではありません。
上記のサンプルコードの中でも、良くない点がきっとあるかと思います。
電卓の実装は他にもいろいろあるかと思います。調べて良い部分を吸収してください。

投稿2017/10/18 13:43

編集2017/10/18 15:41
LouiS0616

総合スコア35658

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

自己解決

もう一度考えてみます

投稿2017/10/18 13:33

Alpa

総合スコア80

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問