Print the previous 30 issues

1

I have to print the 30 numbers before the number chosen by the user.

while(valor > (30 - valor)){
    System.out.println(valor);
    --valor;
}

I did so, but it does not print the previous 30, sometimes yes, sometimes not, where can I be wrong? (it has to be done with while).

    
asked by anonymous 04.06.2017 / 18:21

1 answer

0

Create an auxiliary variable, as long as the value is greater than or equal to itself at least 30, and decreases the value by 1. The problem in your code is that at the same time that you do the validation if valor > 30-valor you also change within the while value, ie, assuming 60 is the value, at each loop this happens:

1st 60 > 30 - 60 // true

2nd 59 > 30 - 59 // Yes

3rd 58 > 30 - 58 // Yes

4th ...

With the auxiliary var it looks like this:

int aux = valor-30;
while(valor >= aux){
   System.out.println(valor);
   --valor;
}
    
04.06.2017 / 18:39