Dong Jun Dong Jun - 13 days ago 5
Java Question

Creating a loop for Scanner for arrays

I want to create a loop that asks for the Scanner to input each number one after the another for a certain amount in an array (I'm thinking of 10). Any suggestions?

import java.util.Scanner;

public class AssignSeven
{
public static void main(String[] args)
{
int [] array1 = new int[10];
System.out.println("Enter 10 numbers");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();

for (int i = 0; i < 9; i++)
{
array1[i] = a;
}

}
}

Answer

change to

    for (int i = 0; i < 9; i++)
    {
        int a = sc.nextInt();
        array1[i] = a;
    }

or even

    for (int i = 0; i < 9; i++)
    {
        array1[i] = sc.nextInt();
    }