How to 'increment' in variables

2

In the code below my variable y asks for the user to enter values until y reaches 10, I make the storage in the variable x and the last information entered is currently stored (which shows at the end of the code) what I would like to know it is like doing an increment in var x, storing the information typed in x1, x2, x3..to x10, as with Y.

import java.util.Scanner;
    public class SEP_06_exe2_p1{
        public static void main(String args[]){
             int x;
             int y = 1;
             do{         
                 Scanner input = new Scanner(System.in);
                 System.out.print("Digite o " + y + "º valor: ");
                 x = input.nextInt();
                 y = ++y; //incrementa 1 no y 
             }while( y <= 10);
             System.out.print(x);
        }
    }
    
asked by anonymous 06.10.2014 / 18:28

2 answers

3

Basically what you need to do is to use a array

> ( Java tutorial and class documentation ). It is a variable that holds multiple values (a collection of values). Then you access each individual value through its index. It would be something like this (I did not test, I do not know if what you did is working, I put it to illustrate what you need to use):

import java.util.Scanner;
public class SEP_06_exe2_p1 {
    public static void main(String args[]) {
        int[] x = new int[10];
        int y = 0;
        Scanner input = new Scanner(System.in);
        do {         
            System.out.print("Digite o " + (y + 1) + "º valor: ");
            x[y] = input.nextInt();
            y++; //incrementa 1 no y <== estava errado veja nota abaixo
        } while (y < 10);
        y = 0;
        do {         
            System.out.println(x[y]);
            y++;
        } while (y < 10);
    }
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

The ++ operator generates side effects, that is, it modifies the state of the variable itself, so you do not need to assign the result to a variable. y++ is the same as saying y += 1 than the same as y = y + 1 . You can simplify this further, but as you are beginning and there are controversies if this simplification is good, I will not quote it.

Arrays always start from scratch. For this I changed the initial value of y and the comparison of while to finish before reaching 10, ie it goes from 0 to 9.

You could use a for flow control in this case, it simplifies a bit. But take one step at a time.

    
06.10.2014 / 19:06
0

You can try to use the code below, which will sum (guardX) the values of x as with the variable y:

import java.util.Scanner;
    public class SEP_06_exe2_p1{
        public static void main(String args[]){
             int x;
             int guardaX = 0;
             int y = 1;
             do{         
                 Scanner input = new Scanner(System.in);
                 System.out.print("Digite o " + y + "º valor: ");
                 x = input.nextInt();
                 guardaX = x + guardaX;
                 y = ++y; //incrementa 1 no y 
             }while( y <= 10);
             System.out.println(x);
             System.out.println(guardaX);
        }
   }
    
06.10.2014 / 18:45