Basic calculator for android

I have a basic problem i write a basic calculator it works but i pressed then equal symbol if i press a new number first answer stay there and new answer write up last answer my english not very well so i will upload a photo i hope you can help me

And my codes:

String input = "";

float input1 = 0;
float input2 = 0;

int operator = 0;

void setup() {

  background(255);
  size(720,1280);
  
  fill(95, 158, 160);
 
  rect(0, height/5, width, height);


  textSize(height/10);
  frameRate(90);
  fill(255);
  text("1", 20, height/4+20, width, height);
  text("2", 20+175, height/4+20, width, height);
  text("3", 20+175+175, height/4+20, width, height);
  text("+", 20+175+175+175, height/4+20, width, height);

  text("4", 20, height/4+255, width, height);
  text("5", 20+175, height/4+255, width, height);
  text("6", 20+175+175, height/4+255, width, height);
  text("-", 20+175+175+175, height/4+255, width, height);

  text("7", 20, height/4+255+255, width, height);
  text("8", 20+175, height/4+255+255, width, height);
  text("9", 20+175+175, height/4+255+255, width, height);
  text("x", 20+175+175+175, height/4+255+255, width, height);

  text("C", 20, height/4+255+255+255, width, height);
  text("0", 20+175, height/4+255+255+255, width, height);
  text("=", 20+175+175, height/4+255+255+255, width, height);
  text("/", 20+175+175+175, height/4+255+255+255, width, height);
}
void draw() { //ekran
  fill(220,20,60);
  text(input, 30, 150);

}

void mousePressed() {
 
  if (mouseX >= 0 && mouseX <= 175 && mouseY >= height/4 && mouseY<= height/4+255 ) {
    input = input + 1;

  } else if (mouseX >= 175 && mouseX <= 0+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    input = input +2;
  
  } else if (mouseX >= 0+175+175 && mouseX <= 0+175+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    input = input +3;
   
  } else if (mouseX >= 0+175+175+175 && mouseX <= 0+175+175+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    println("+");
    input1 = parseInt(input);
    input = "";
    input = input + "+";
    operator = 1;
  noStroke();
    fill(255);
    rect(0, 0, width, height/4);
    // rad 2
  } else if (mouseX >= 0 && mouseX <= 175 && mouseY >= height/4+255 && mouseY<= height/4+255+255) {
    input = input +4;
  
  } else if (mouseX >=175 && mouseX <= 175+175 && mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    input = input +5;
  
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175&& mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    input = input +6;
  
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175&& mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    println("-");
    input1 = parseInt(input);
    input = "";
    fill(255);
    rect(0, 0, 667, 212);
    input = input + "-";
    operator = 2;
    

    //rad 3
  } else if (mouseX >= 0 && mouseX <= 175&& mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    input = input +7;

  } else if (mouseX >= 175  && mouseX <= 175+175 && mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    input = input +8;

  } else if (mouseX >= 175+175 && mouseX <= 175+175+175 && mouseY >= height/4+255+255&& mouseY<= height/4+255+255+255 ) {
    input = input +9;
  
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175 && mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    println("x");
    input1 = parseInt(input);
    input = "";
    fill(255);
    rect(0, 0, 667, 212);
    input = input + "";
    operator = 3;

    //rad 4
  } else if (mouseX >= 0 && mouseX <= 0+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    println("C");
    input = "";
    input1 = 0;
    input2 = 0;
    operator = 0;
    fill(255);
    rect(0, 0, 665, 200);
  
  } else if (mouseX >= 175 && mouseX <= 175+175 && mouseY >= height/4+255+255+255&& mouseY<= height/4+255+255+255+255+255) {
    input = input +0;
 
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175  && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    println("=");
    input2 = parseInt(input);
    input = input + "=";
   
    fill(255);
    rect(0, 0, 900, 300);
    fill(220,20,60);

    if (operator == 1) {
      text(int(input1 + input2), 30, 150);
      operator = 0;
    } else if (operator == 2) {
      text(int(input1 + input2), 30, 150);
    } else if (operator == 3) {
      text(int(input1 * input2), 30, 150);
    } else if (operator == 4) {
      text(nf(input1 / input2,0,0), 30, 150);
      if (input2 == 0)
;
    }
    input = "";
    input1 = 0;
    input2 = 0;
    operator = 0;
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    input1 = parseInt(input);
    input = "";
    fill(255);
    rect(0, 0, 667, 212);
    input = input + "";
    operator = 4;
  
  }
  println(input);
}
1 Like

The problem here is how you draw the things.
You draw all of the buttons and everything inside of setup(), which is run only once, and then you draw just the input text inside of draw(). As a result, there is nothing to clear the stuff you draw to the screen, so it just draws ontop of it again and again. In other words: in that image, you have 45 ontop of 71 - what is there to remove 71 before drawing 45?

As a fine fix, I suggest either moving background(..., text(... and other functions into draw() instead. Or, in this case, you can just draw white on the upper part of the screen.

void draw() { //Π­ΠΊΡ€Π°Π½... Π― русский, Π½ΠΎ Ρ„ΠΎΡ€ΡƒΠΌ англоязычный, ΠΈ Ρ‡Ρ‚ΠΎΠ±Ρ‹ большС людСй ΠΏΠΎΠ½ΠΈΠΌΠ°Π»ΠΈ я Π½Π° английском. Π’Π°ΠΊ Ρ‡Ρ‚ΠΎ, поТалуйста, ΠΎΡ‚Π²Π΅Ρ‡Π°ΠΉΡ‚Π΅ Π½Π° английском. :v
fill(255);
rect(0,0,width,height/5);
fill(220,20,60);
text(input, 30, 150);
}

This does not fix the problem completely, as the drawn result gets drawn over with that rect(... pretty quickly, but it should give you an idea of what to do next to fix it.
Hints: Use variables, store your result in a variable, add boolean displayResult and set it to true when the user presses =, and use if(displayResult) inside of draw() to know when to display input or when to display result! :smiley:

P.S., 4th and 5th arguments of text are used only if your text is really long and needs to move part of it to the next line - your text is 1 characters long and you are giving it huge boundaries, so you can just ignore it.

2 Likes

Thank you but my english not very well can you write this code for me ?because i didnt understand completly.

Oh. Here’s the code with my hints and solutions applied:

String input = "";

float input1 = 0;
float input2 = 0;
float result = 0;

int operator = 0;

boolean drawResult = false;

void setup() {

  background(255);
  size(720, 1280);

  fill(95, 158, 160);

  rect(0, height/5, width, height*4/5);

  textSize(height/10);
  frameRate(15); //frameRate of 90 is unneeded for a calculator and drains the battery! You won't even notice the difference, trust me.
  fill(255);
  text("1", 30, height/5*1.7);
  text("2", 30+175, height/5*1.7);
  text("3", 30+175+175, height/5*1.7);
  text("+", 30+175+175+175, height/5*1.7);

  text("4", 30, height/5*2.7);
  text("5", 30+175, height/5*2.7);
  text("6", 30+175+175, height/5*2.7);
  text("-", 30+175+175+175, height/5*2.7);

  text("7", 30, height/5*3.7);
  text("8", 30+175, height/5*3.7);
  text("9", 30+175+175, height/5*3.7);
  text("x", 30+175+175+175, height/5*3.7);

  text("C", 30, height/5*4.7);
  text("0", 30+175, height/5*4.7);
  text("=", 30+175+175, height/5*4.7);
  text("/", 30+175+175+175, height/5*4.7);
}
void draw() { //ekran
  fill(255);
  rect(0, 0, width, height/5);
  fill(220, 20, 60);
  if (drawResult) {
    text(nfc(result,3), 30, 150);
  } else {
    text(input, 30, 150);
  }
}

void mousePressed() {

  if (mouseX >= 0 && mouseX <= 175 && mouseY >= height/4 && mouseY<= height/4+255 ) {
    input = input +1;
    drawResult = false;
  } else if (mouseX >= 175 && mouseX <= 0+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    input = input +2;
    drawResult = false;
  } else if (mouseX >= 0+175+175 && mouseX <= 0+175+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    input = input +3;
    drawResult = false;
  } else if (mouseX >= 0+175+175+175 && mouseX <= 0+175+175+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    println("+");
    drawResult = false;
    input1 = parseInt(input);
    input = "+";
    operator = 1;
    //noStroke();
    //fill(255);
    //rect(0, 0, width, height/4);
    // rad 2
  } else if (mouseX >= 0 && mouseX <= 175 && mouseY >= height/4+255 && mouseY<= height/4+255+255) {
    input = input +4;
    drawResult = false;
  } else if (mouseX >=175 && mouseX <= 175+175 && mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    input = input +5;
    drawResult = false;
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175&& mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    input = input +6;
    drawResult = false;
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175&& mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    println("-");
    drawResult = false;
    input1 = parseInt(input);
    input = "-";
    //fill(255);
    //rect(0, 0, 667, 212);
    //input = input + "-";
    operator = 2;

    //rad 3
  } else if (mouseX >= 0 && mouseX <= 175&& mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    input = input +7;
    drawResult = false;
  } else if (mouseX >= 175 && mouseX <= 175+175 && mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    input = input +8;
    drawResult = false;
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175 && mouseY >= height/4+255+255&& mouseY<= height/4+255+255+255 ) {
    input = input +9;
    drawResult = false;
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175 && mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    println("x");
    drawResult = false;
    input1 = parseInt(input);
    input = "x";
    //fill(255);
    //rect(0, 0, 667, 212);
    operator = 3;

    //rad 4
  } else if (mouseX >= 0 && mouseX <= 0+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    println("C");
    drawResult = false;
    input = "";
    input1 = 0;
    input2 = 0;
    operator = 0;
    //fill(255);
    //rect(0, 0, 665, 200);
  } else if (mouseX >= 175 && mouseX <= 175+175 && mouseY >= height/4+255+255+255&& mouseY<= height/4+255+255+255+255+255) {
    input = input +0;
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    println("=");
    input2 = parseInt(input.substring(1)); //.substring(1) removes 1 symbol from the text
    input = input + "=";

    //fill(255);
    //rect(0, 0, 900, 300);
    //fill(220,20,60);
    if (operator == 1) {
      result = input1 + input2;
    } else if (operator == 2) {
      result = input1 - input2;
    } else if (operator == 3) {
      result = input1 * input2;
    } else if (operator == 4) {
      result = input1 / input2;
    }
    drawResult = true;
    input = "";
    input1 = 0;
    input2 = 0;
    operator = 0;
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    input1 = parseInt(input);
    input = "";
    fill(255);
    //rect(0, 0, 667, 212);
    input = input + "";
    operator = 4;
  }
  println(input);
}

Here, fancied the code a little.
Good luck with coding stuff!

1 Like

Thank you very much :grinning:πŸ–‘

This time divided button not working how i fix it?