Android開発の勉強として電卓を作っているのですが、2+2=4もう一度=を押すと6、更に押すと、8、10・・・という具合に計算してくれる機能を実装したいのですがどのようにすればいいか全く見当がつきません、ご教授よろしくお願いします、
switch(buttonStr) { case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": /** case "0": 〜 case "9": の時に実行する処理*/ if (firstNum) { System.out.println(firstNum); System.out.println(""); if (!operator.equals("")) { operand1 = display; } display = "0"; firstNum = false; } System.out.println(firstNum); /** maxKeta(20桁)を超えたらOVERFLOW表示 */ if (display.length() >= maxKeta) { textViewDisplay.setText(OVERFLOW); break; } displayNum = Long.parseLong(display); long buttonNum = Long.parseLong(buttonStr); displayNum = displayNum * 10 + buttonNum; textViewDisplay.setText(Long.toString(displayNum)); break; case "+": case "−": case "×": case "÷": case "=": if (!operator.equals("") && !operand1.equals("") && !firstNum) { System.out.println("### 最初に=おされたよ ###"); long operand1Long = Long.parseLong(operand1); long operand2Long = Long.parseLong(display); double operand1Double = (double)operand1Long; double operand2Double = (double)operand2Long; switch(operator) { case "+": if (Math.abs(operand1Double + operand2Double) > maxDouble) { textViewDisplay.setText(OVERFLOW); } else { textViewDisplay.setText(Long.toString(operand1Long + operand2Long)); } break; case "−": if (Math.abs(operand1Double - operand2Double) > maxDouble) { textViewDisplay.setText(OVERFLOW); } else { textViewDisplay.setText(Long.toString(operand1Long - operand2Long)); } break; case "×": if (Math.abs(operand1Double * operand2Double) > maxDouble) { textViewDisplay.setText(OVERFLOW); } else { textViewDisplay.setText(Long.toString(operand1Long * operand2Long)); } break; case "÷": if (operand2Long == 0L) { textViewDisplay.setText(ZERODIV); } else { textViewDisplay.setText(Long.toString(operand1Long / operand2Long)); } break; default: Log.d("ERROR", "operatorが識別できない:" + operator); } } if (buttonStr.equals("=")) { System.out.println("### =押されたよ ###"); operator = ""; } else { operator = buttonStr; } firstNum = true; break; case "AC": textViewDisplay.setText("0"); operand1 = ""; operator = ""; break; default: Log.d("ERROR", "識別できないボタンが押された:" + buttonStr); } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/19 03:27