简体   繁体   中英

How to place number next to another number

My homework for a Java is making a GUI calculator in Java. I am in the planning of the program process and wondering how can you add a number next to another number and make it a single integer. I know there is probably a name for this but i cant remember it or i just don't know it.

if x = 1, and y = 2, then z should equal 12

How do i do this?

Thanks in advance!

int z = Integer.parseInt(x+""+y) is the correct approach. If we need to take a negative number in the calculator application, the -ve sign will be before the first digit entered, which works well here. Also in Calculator only one digit will be added at one time, so this approach works for negative numbers as well.

I believe this is what you're looking for.

    int x = 1;
    int y = 2;
    String answer = x + "" + y;
    int z = Integer.parseInt(answer);

    System.out.println(z); //Should print 12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM