Ask coding questions

← Back to all posts
Why does it not let me type a number?[Java]
IGamer123 (56)

The code works, but when you run it, it immediately prints the 2nd line as well, not letting me type a number for it to calculate.

Comments
hotnewtop
Wumi4 (504)

Bruh, you haven't asked user the input for the number yet, you just print the prompt.

IGamer123 (56)

Better? @Wumi4

import java.util.Scanner;

public class SumOfDigits {

public static void main(String[] args) {

int N = 0;
int x = 0;
int sum = 0;

Scanner keyboard = new Scanner(System.in);

System.out.println("******");
System.out.println();
System.out.print("Please enter a positive integer number N: ");

N = keyboard.nextInt();

int M = N;

while(N > 0) {

    x = N % 10;

    sum = sum + x;

    N = N / 10;
}


System.out.println();
System.out.println("Sum of all digits of the number " + M + " is: " + sum);
System.out.println();
keyboard.close();
}

}