Store input values in Java

0

I need to store values with more than one entry. If a program requests 10 entries, should I store in 10 variables? In case the entries are of type Int .

    
asked by anonymous 30.03.2018 / 02:16

1 answer

3

Yes, you should, but understand what a variable .

Doubt does not talk about which values will be entered, but it is likely that these variables are of type int , since for exercises like this it usually asks for only integer numeric values.

But most likely you are expected to use an array . An array variable is one that stores other variables in sequence. Then we will have 11 variables, 1 for the array and 10 for the integers.

In languages typed like Java all variables have types at all. So I'm afraid an array of integers.

When we are using an array we are talking about accessing a value that depends on variations: one is the array and another is the given itself. So to access an array just use the name of the variable, but to access the value it is necessary to use the variable name of the array and the index that indicates which variable will be used .

So let's create the array of type int with 10 positions.

int[] array = new int[10];

Now to access the first variable of it we use:

array[0]

For the second:

array[1]

Until the last:

array[9]

This is how to access array0 , array1 , array9 , but if you used the names so the index could not vary and this leaves the application less flexible. With the index it is possible to use a variable in it, like this:

array[i]

What position are you picking up? The one that the variable i indicates, varies, and this is the beauty of computing. So you can manipulate 10 variables in up to one line by making a loop of repetition.

Are not you accustomed to array , loop, these things? There is enough information here on the site that can help you, give one searched.

    
30.03.2018 / 02:40