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());
}
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.
– Matheus Guedes