How to recover a previous value typed by the user?

Asked

Viewed 224 times

1

I am not able to recover a value typed before by the user.

The program asks to enter with notes and the end to be indicated by the entry of the number -1;

public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        ArrayList<Integer> notas = new ArrayList<>();

        System.out.println("Entre com notas de 0 a 10");
        int num = 0; // criei essa variavel para fazer as instruçoes ficarem em loop ate o usuario digitar o -1 

        while(num != -1){ 

            notas.add(s.nextInt()); //aqui armazena o primeiro valor digitado

            if(s.nextInt() == -1){ // nesta linha eu sei que que esta pegando o segundo valor digitado e se caso ele seja -1, atribuo o valor -1 a num, saindo assim do loop
                num = -1;
            }else{ //caso o valor nao seja -1 vai adicionar no ArrayList
                notas.add(s.nextInt()); //aqui esta o problema, eu nao consigo adicionar o valor anterior que foi comparado no if
                }
        }

        System.out.println(notas.toString());

    }
  • 1

    I can’t recover a typed amount before, you know. For example: I typed note 1, it is being stored, when I am typing note 2 it is being compared in if with -1. As it is not the same I go to Isis, however it is not storing note 2 but the next note. More or less that. Thank you.

2 answers

1


One solution could be this:

import java.util.*;

class Main {
    public static void main (String[] args) {
        var s = new Scanner(System.in);
        var notas = new ArrayList<Integer>();
        System.out.println("Entre com notas de 0 a 10");
        var num = 0;
        while (num != -1) { 
            num = s.nextInt(); //armazena em variável para poder usar onde quiser
            if (num != -1) notas.add(num);
        }
        System.out.println(notas.toString());
    }
}

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

The main function of a variable is to store a value for later use. Saving variable is good, but when it is needed, you have to create and store the value you want to use later. Thus solves the main question the question. But in fact the variable already created can be used for this and not create an extra. Besides your code doesn’t do what you expect, so I changed the logic.

  • I will test here friend. I’ll be back to talk about the result. Thank you very much.

  • @Matheusguedes gave him an improved one. In this case it was easier to do as it is now. And it doesn’t introduce a command that maybe you haven’t even learned yet.

  • Very good friend. It worked very well.

0

Just do like this:

int temp = s.nextInt();
if(temp == -1){
   num = -1;
}else{ //caso o valor nao seja -1 vai adicionar no ArrayList
  notas.add(temp); //pega o ultimo valor digitado
}
  • I’ll test here and come back to talk. Thank you very much.

Browser other questions tagged

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