I have made a program that asks:
The length of the array:
and the elements of the Array:
And then the program will multiply the elements.
My question is, how would I prompt the user to just enter the elements of the array without asking for the length and then stop when I type -1? Would I need to use an array list? Here is my code so far.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Length of the array: "); //Prompt User for length
int number = Integer.parseInt(input.next());
System.out.print("The elements of your array: "); //Prompt User for elements
int[] myArray = new int[number];
for(int i = 0; i < number; i++){
myArray[i] = Integer.parseInt(input.next());
}
System.out.printf("The multiplication is %d.", multiplication(myArray, myArray.length - 1) ); //End Statement
}
static int multiplication(int[] array, int startIndex) {
if (startIndex == 0)
return(array[startIndex]);
else
return (array[startIndex] * multiplication(array, startIndex - 1));
}
}
Any help would be appreciated.