I’m trying to make a program that prompts the user for 2 items – a count of values, and a first value. With this information, we build an array of the entered size, and fill it with integers starting from the users start value.
I am having difficulty working out how to start filling the array from the user’s input. Could someone please help me to understand what needs to be done?
This is what the interaction should look like…
- Enter count:
- 4
- Enter a starting value:
- 2
- [2, 3, 4, 5]
And this is my code so far…
import java.util.Scanner;
import java.util.Arrays;
public class Problem1b {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a count:");
final int num = input.nextInt();
int[] count = new int[num];
System.out.println("Enter a starting value:");
int index = input.nextInt();
for (int i = index; i < count.length; i++){
count[i] = i;
}
System.out.println(Arrays.toString(count));
}
}
Change the loop: