How to sum up any two numbers entered by the user through a space? (Java) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to sum up any two numbers entered by the user through a space? (Java)

The user enters any two numbers through and needs to sum them up. (Java)

23rd Apr 2018, 3:59 PM
Руслан Соколов
Руслан Соколов - avatar
1 Answer
+ 2
There are many ways to do this, Arrays or ArrayLists of type string or integer from the inputs using "scanner". From there just use a "for loop" to get the first index value and the one next to it, and sum them into a variable, and then move to the next index and sum it again until you meet the "length" of the array or the "size()" of the ArrayList. Something like this: for (int i = 0; i < array.length; i++){ variable += array[i]; } Just clean the inputs so that that every value get into aN index as expected.
23rd Apr 2018, 4:13 PM
Roberto Guisarre
Roberto Guisarre - avatar