CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Append digit to end of variable (http://www.codingforums.com/showthread.php?t=148384)

Crazydog 09-08-2008 12:16 AM

Append digit to end of variable
 
Hey everyone.
I have this assignment for school where I have to create a very simple calculator program. No GUI, not even command line inputs. It's all being run inside the program with preset commands.

Here's the problem:
I can't figure out how to append a digit to the end of a variable.
The calculator must work like this:

(it is java, not php. The highlighting makes it easier to read)
PHP Code:

//press "5"
calc.digit(5);
//press "3"
calc.digit(3);
//Should display: "53"
System.out.println(calc.display()); 

I can't for the life of me figure out how to set 5 to a variable, and then add 3 to the end of that variable. Can anyone help? Many thanks.

Note: the digit method requires the number to be an int

icm9768 09-08-2008 03:06 AM

A fairly straight forward approach would be to multiply your existing number by 10 and then add the new digit.

So in your example below the user enters 5. Since your initial number would be zero the operation would be ( 0 * 10 ) + 5. Now your calculator input is five. Then the user enters 3 so the next operation would be ( 5 * 10 ) + 3. Now the user input is 53 and so on and so forth.

Crazydog 09-08-2008 03:30 AM

Thanks. I was thinking about that in the first place, but didn't put too much thought into it, since I assumed there would be a more practical way.


All times are GMT +1. The time now is 05:33 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.