I do not understand what is the error

import java.util.Scanner; public class Zadachi{ public static void main(String[] args){ int a; long b=1; Scanner scn=new Scanner(System.in); System.out.println("Введите натуральное число"); if(scn.hasNextInt()){ a=scn.nextInt(); for(int i=a;i>0;i--){ } System.out.println("Факториал числа "+a+" равен "+b); } else System.out.println("Ошибка. Введено не число"); } 

Closed due to the fact that off-topic participants default locale , Kromster , Viktorov , MSDN.WhiteKnight , Edward 18 Jan '18 at 18:06 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - default locale, Kromster, Viktorov, MSDN.WhiteKnight, Edward
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    You have an empty for loop - is that necessary? - VladD
  • 2
    Write what exactly goes wrong (the code is not compiled? An exception occurs when executing? The result is not the same? For what value?) - default locale
  • Thanks for the help - Terletskiy Alexander

2 answers 2

The presented solution is not entirely correct. If you enter, for example, the number 123, the result will be 0. If you donate the work time, you can rewrite to BigInteger and then the result will be correct.

Here is a sample code:

 import java.math.BigInteger; import java.util.Scanner; public static void main(String[] args) { int a; BigInteger bi = BigInteger.ONE; Scanner scn = new Scanner(System. in ); System.out.println("Введите натуральное число"); if (scn.hasNextInt()) { a = scn.nextInt(); for (int i = a; i > 0; i--) { bi = bi.multiply(BigInteger.valueOf(i)); } System.out.println("Факториал числа " + a + " равен " + bi); } else { System.out.println("Ошибка. Введено не число"); } } 

    Thanks VladD, found a mistake, I had an empty for, for some reason I missed this moment. I give you the right solution to my problem.

     import java.util.Scanner; public class Zadachi{ public static void main(String[] args){ int a; long b=1; Scanner scn=new Scanner(System.in); System.out.println("Введите натуральное число"); if(scn.hasNextInt()){ a=scn.nextInt(); for(int i=a;i>0;i--){ b*=i; } System.out.println("Факториал числа "+a+" равен "+b); } else System.out.println("Ошибка. Введено не число"); } }