So, I'm learning to develop in Java. The teacher asked me to develop a method for the Factorial. And also show what is happening on the screen, for example:
3! = 3 X 2 X 1 = 6
In fact, I made it happen, but is there any way for the code to get better? :
int n = Integer.parseInt(txtN.getValue().toString());
int f = 1;
int c = n;
String s = "";
while(c>=1){
f *= c;
if (c == 1) {
s += c;
} else if (c>1) {
s += c + " x " ;
}
c--;
}
lblFat.setText(s + " = " + Integer.toString(f));
The code generates the factorial and should also show the numbers on the screen.
Example
3! = 3 X 2 X 1 = 6
3! -> txtN (Local para o usuário inserir o número)
6 -> lblFat (local que gera o resultado)
Factorial in Java, do you have a better way to write code?