Domanda

I am making a calculator with the processing programing language. The part I am stuck on though is making it so that you can have numbers in the tens, hundreds, thousandths place and so on. Right now all I can do is add/subtract with two one digit numbers. I have searched for an answer to this everywhere. Since I cannot find anything on it I feel like this is a very easy thing I am just acting stupid.

È stato utile?

Soluzione

If i got you, I've been there once, the way I found to achieve this kind of input of normal calculators is using strings for input converted later to floats or ints. I was dealing with timecode, so there was no floats or dots. You need to add that, but the idea is:

[edit] added a simple dot input handler, it seams to work:)

StringBuilder buff = new StringBuilder("");

void draw(){}

void keyReleased()
{

  if(key != CODED){
    char c = key;
    if ( c >= '0' && c <= '9' || c == '.'){
      buff.insert(buff.length(),c);
    }
    println("buff = " + buff);
  }
  print("in float... plus 100.25 equals: ");
  println(100.25 + float(buff.toString()));
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top