In descending order, remove a value from the entered number up to 0

1

I need the program to remove a value from the number entered by the user until it reaches 0. I do not know if the replay loop used should be for, but my program looked like this.

package numerodescrecente;

import java.util.Scanner;

public class NumeroDescrecente {

    public static void main(String[] args) {
        Scanner leitor = new Scanner(System.in);
        System.out.println("DIGITE UM NUMERO");
        int n1 = leitor.nextInt();
        for (n1=n1;n1>0;n1--)
        {
            System.out.println("Numero: "+n1);
        }
    }

}
    
asked by anonymous 25.08.2018 / 20:57

1 answer

0

Your program works. See it working on ideone. With input 12, it produces this output:

DIGITE UM NUMERO
Numero: 12
Numero: 11
Numero: 10
Numero: 9
Numero: 8
Numero: 7
Numero: 6
Numero: 5
Numero: 4
Numero: 3
Numero: 2
Numero: 1

However, there are a few things to review in your code:

  • n1=n1 does nothing at all. It is best to set int n1 = leitor.nextInt() to the for initialization statement.

  • If the idea is to go to 0, use n1 >= 0 .

Your code should look like this:

package numerodescrecente;

import java.util.Scanner;

public class NumeroDescrecente {
    public static void main(String[] args) {
        Scanner leitor = new Scanner(System.in);
        System.out.println("DIGITE UM NÚMERO");
        for (int n1 = leitor.nextInt(); n1 >= 0; n1--) {
            System.out.println("Número: " + n1);
        }
    }
}
    
25.08.2018 / 21:21