9
So I’m learning to develop in Java. The teacher asked me to develop a method for Factorial. And also show on the screen what is happening, for example:
3! = 3 X 2 X 1 = 6
In fact, I made it happen, but there is some way the code gets 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 must 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, has a better way of writing code?
You can use the Lanczos :D approach
– epx