add - adding an unknown number of numbers in java -
i have following code below:
public class classes { /** * @param args command line arguments */ public static void main(string[] args) { int sum = add(10,20); system.out.println(sum); // todo code application logic here } public static int add(int number,int number2){ int c = number + number2; return c; } }
in case adding 2 numbers , returning them main method. want alter this, keeping same structure, way of adding unspecified number of numbers. example if wanted add 3 numbers adjust following to
public static int add(int number,int number2,int number3){ int c = number + number2 + number3;
i cant keep adding int numberx public static int add(
so do?
you can submit array of integers , loop , summarize them.
public static int add(integer... numbers) { int result = 0; (integer number : numbers) { result += number; } return result; }
Comments
Post a Comment