So for arrays I'm trying to ask the user to input 10 numbers between 1 and 100 into an array. I'm having some trouble wrapping my head around this. I do not understand how to set an array size AND have it call from a variable associated with user input at the same time. Is it even possible to do this? I'm not asking for a solution but maybe just a better way to understand this. My school book does not help much.
Asked
Active
Viewed 156 times
-1
-
What language are you using? – Loocid Apr 02 '15 at 01:56
-
Even I don't understand how it is possible unless you tell us the programming language in which it is supposed to be done. – shauryachats Apr 02 '15 at 01:56
-
java, sorry I'll add that do the main post. – neito Apr 02 '15 at 01:56
-
Check this link if you are getting input count from user and need dynamic array -- http://stackoverflow.com/questions/1647260/java-dynamic-array-sizes – Berke Cagkan Toptas Apr 02 '15 at 02:07
2 Answers
2
You can try this:
int[] arr = new int[10];
int pos = 0;
Scanner in = new Scanner(System.in);
while (pos < 10) {
System.out.print("input a number(1-100):");
int a = in.nextInt();
if (a > 0 && a <= 100) arr[pos++] = a;
}
And don't forget to import java.util.Scanner;

PageNotFound
- 2,320
- 2
- 23
- 34
-
`ask the user to input 10 numbers` - why the array and loop of 100? – Scary Wombat Apr 02 '15 at 02:01
-
-
0
you need to provide more information with your question. What language you are using? How is your program structure?
Since you already know your array size and everything it is very easy to start.
Firstly declare your array. Since i don't know what language you are using, i will just explain in c++ and java.
C++ Example
int array_numbers[10]; // Which is 0-9.
Then you go ahead and prompt the user for an input.
cout << "Please enter 10 numbers : ";
Once this is done, the user is now going to input 10 numbers. For this you do a loop with the command to store the values.
for(int x=0;x<=9;x++) //
{
cin >> array_numbers[x]; //entering the input into the variable index x
}
finally to display the value:
for(int x=0;x<=9;x++)
{
cout << "array_numbers[" << x+1 << "] is : " << array_numbers[x];
//x+1 to show the index to the user.
}
Java Example
and another example in java
Declaring the variable and the scanner :
int[] array_numbers = new int[10];
Scanner scan = new Scanner(System.in);
//import java.util.Scanner; <- don't forget to do this in the beginning
Then the loop :
for(int x=0;x<=9;x++)
{
array_numbers[x] = scan.nextInt();
}
and lastly to display the results:
for(int y=0; y<=9; y++)
{
System.out.println("array_numbers["+ (y+1) +"] is : "+ array_numbers[y]);
}
Please do comment on this if you need further help.

Sangkaran
- 38
- 1
- 7