How to increment variables

Asked

Viewed 353 times

2

In the code below my variable y asks the user to enter values until y reaches 10, I store the variable x and currently the last information typed is stored (which shows at the end of the code) what I would like to know is how to make an "increment" in var x, storing the information typed in X1, x2, X3. up 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);
        }
    }
  • This increment you are talking about is called filling an array. If you have x as int you will never be able to save values

  • From what I understand you’re starting with this language, and without wishing to distort the study plan, I’d like to answer before you can use Arrays to resolve this citation?

2 answers

3


Basically what you need to do is use a array (java tutorial and class documentation). It is a variable that stores several values (a collection of values). Hence you access each individual value through its index. It would be something more or less like this (I did not test, I do not know if what you did is working, I put 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);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The operator ++ generates side effect, 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 thing as saying y += 1 that the same as saying y = y + 1. There’s a way to simplify this even more, but since you’re starting out and there are controversies if this simplification is good, I won’t quote you.

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

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

  • What would I call seeing (testing) these variables stored in x for future validation? ex: System.out.printf(?????);

  • Do you want to move on to another method as in the other question? If it is, surely, just pass the x. Of course the method has to be waiting for an argument of the type array. What directly is not the case with print. But then maybe that would be another question.

  • Yes, p be more exact q wish is 'Do a Prog that reads 10 values and prints ordered', in the other Prog I read the values and print the largest, I did not use array, I must use array to understand it, in this case I am trying to decrease the lines of code, instead of using 10 vars I thought to use as above the|while p store the information and pass to validation via array the values. Later I will decrease the size of the validation too, but I’m going step by step because I want to understand correctly, if you know any site q have some explanations that can help me would be great! Thank you

0

You can try using the code below, which will perform a sum(guardaX) of 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);
        }
   }
  • I don’t think you understand what the AP is needing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.