I am writing a calculator on android, but I can’t do the function of percent and exponentiation (any number to any), because after pressing the button itself, the program immediately counts (assigns the value of the second variable to 0), and must wait for the second variable to be entered from the buttons (keyboard) .

Of course, I use the Math.pow(a,b) method (for exponentiation) and the buttonExponentiation button is in the general switch/case .

How to make this delay (so that the button waits for the input of the second variable)?

 public void onClick(View view) { switch (view.getId()) { case R.id.button1: ClickNumber(1); break; case R.id.button2: ClickNumber(2); break; case R.id.button3: ClickNumber(3); break; case R.id.button4: ClickNumber(4); break; case R.id.button5: ClickNumber(5); break; case R.id.button6: ClickNumber(6); break; case R.id.button7: ClickNumber(7); break; case R.id.button8: ClickNumber(8); break; case R.id.button9: ClickNumber(9); break; case R.id.button0: ClickNumber(0); break; case R.id.buttonAdd: if (flagAction == 0) flagAction = 1; break; case R.id.buttonSub: if (flagAction == 0) flagAction = 2; break; case R.id.buttonMult: if (flagAction == 0) flagAction = 3; break; case R.id.buttonDiv: if (flagAction == 0) flagAction = 4; break; case R.id.buttonEq: switch (flagAction) { case 1: result = operand1 + operand2; showNumber(result); operand1 = result; break; case 2: result = operand1 - operand2; showNumber(result); operand1 = result; break; case 3: result = operand1 * operand2; showNumber(result); operand1 = result; break; case 4: result = operand1 / operand2; showNumber(result); operand1 = result; break; default: Toast.makeText(this, "Вкажіть дію!", Toast.LENGTH_LONG).show(); } if (flagAction != 0) { showNumber(result); clearVariables_2(); } break; case R.id.buttonClean: clearVariables(); showNumber(operand1); break; case R.id.buttonComa: break; case R.id.buttonSquare: if (flagAction == 0) { result = Math.pow(operand1, 2); showNumber(result); // clearVariables_2(); operand1 = result; } else { Toast.makeText(this, R.string.other_operation, Toast.LENGTH_LONG).show(); } break; case R.id.buttonSqrt: if (flagAction == 0) { result = Math.sqrt(operand1); showNumber(result); operand1 = result; } else { Toast.makeText(this, R.string.other_operation, Toast.LENGTH_LONG).show(); } break; case R.id.buttonBack: // !!!!!!!!!!!!!! NULL WHY ??????????? String Temp = null; if (flagAction == 0) { if (operand1 % 1 == 0) { Temp = Integer.toString((int) operand1); } else { Temp = Double.toString(operand1); } Temp = Temp.substring(0, Temp.length() - 1); if (Temp.length() > 0) { operand1 = Double.parseDouble(Temp); } else { operand1 = 0; } showNumber(operand1); } else { if (flagAction == 0) { if (operand2 % 1 == 0) { Temp = Integer.toString((int) operand2); } } else { Temp = Double.toString(operand2); } Temp = Temp.substring(0, Temp.length() - 1); if (Temp.length() > 0) { operand2 = Double.parseDouble(Temp); } else { operand2 = 0; } showNumber(operand2); } break; case R.id.buttonOff: finish(); break; case R.id.buttonExponentiation: if (flagAction == 0) { if(operand1 != 0){ // some code .................. } result = Math.pow(operand1, operand2); showNumber(result); } else { Toast.makeText(this, R.string.other_operation, Toast.LENGTH_LONG).show(); } break; case R.id.buttonPercent: result = operand1 * 100 / operand2; showNumber(result); break; } } 
  • one
    Show your code. - post_zeew
  • case R.id.buttonExponentiation: if (flagAction == 0) {if (operand1! = 0) {// some code ..................} result = Math. pow (operand1, operand2); showNumber (result); } else {Toast.makeText (this, R.string.other_operation, Toast.LENGTH_LONG) .show (); } break; - Bogdan Tanchak
  • I think this will not be enough. And yes, there is no need to add code snippets to a comment - add it to the answer itself (and do not forget about the code formatting). - post_zeew
  • flagAction is a variable that defines 4 mat. action operand 1 is the first variable operand 2 is the second variable - Bogdan Tanchak
  • one
    You are aware that real calculators work using the reverse Polish notation algorithm - pavlofff

1 answer 1

You need to change the logic of exponentiation (or better the logic of the whole calculator), for example:

 case R.id.buttonExponentiation: if (flagAction == 0) { // здесь нужно присвоить какой-либо флаг flag = true; } else { Toast.makeText(this, R.string.other_operation, Toast.LENGTH_LONG).show(); } break; 

And the check and the code of the construction itself are transferred to pressing the numbers, inside the function ClickNumber

 .....Код присвоения operand1/operand2..... ...... // проверяете присвоенный вами на прошлом шаге флаг и операнды if(flag && operand1 != null && operand2 != null){ // А здесь уже возводите в степень result = Math.pow(operand1, operand2); } 

I looked in Google calculator and I understood what you mean. Zanulyat operand'ы and you need a flag by pressing the keys of the action (not numbers). And operand2 you need to assign something like this:

 if(operand2 == null){ operand2 = keyboardval; } else { operand2 *= 10; operand2 += keyboardval; } 

where keyboardval is the value of your digit

That is: you enter a number, this is your operand1 , enter it until you have pressed any action key, remember operand1 . After that, remember what action was pressed, assign an action flag. If the action flag has been assigned, then when you press the numbers, you already assign them to the variable operand2 (do not forget to check that operand1 != null ). Accordingly, each time you press a digit, you must recalculate the result value until the action button is pressed again - at this very moment you output the result of the operation, assign operand1 = result , operand2 = null . For a simple calculator, this is enough. If you want a more complex version, like Google’s, then you will have to save all entered numbers and operations, and then carry them out by priority.