I would not say that my solution is better, is just different and with a few more details, being a numerical detail and two aesthetic firulias, but in the same algorithm is very similar.
import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int tamanho = getTotalTermos();
long[] numeros = new long[tamanho];
numeros[0]=0;
numeros[1]=1;
System.out.print("0, 1");
for(int i=2; i<numeros.length; i++)
{
numeros[i] = numeros[i-1] + numeros[i-2];
System.out.print(", " + numeros[i]);
}
System.out.println();
}
private static int getTotalTermos()
{
int total_termos;
Scanner input = new Scanner(System.in);
try
{
System.out.print("Digite a quantidade de termos: " );
total_termos = input.nextInt();
if(total_termos<2)
{
System.out.println("Por favor digite um número que seja maior do que 1" );
return getTotalTermos();
}
}
catch(Exception e)
{
System.out.println("Erro - Número inteiro inválido");
return getTotalTermos();
}
return total_termos;
}
}
// Digite a quantidade de termos: 15
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377
More details:
1) I treated the typing of characters that were not numbers asking again to enter a valid number, but this was a violation.
2) Now one detail you may have missed was using int. In my case I used long. Why? - Why the Fibonacci sequence grows very fast and bursts the numerical range of int from -2147483648 to + 2147483647
3) I put a comma separating the numbers. Another thread. :)
4) At the end of my algorithm there will be an array of numbers with the Fibonacci sequence that could be used for something else.
byte = 1 byte - 8 bits = -128-127 - integers
short = 2 bytes - 16 bits = -32768 to +32767 - integers
int = 4 bytes - 32 bits = -2147483648 a + 2147483647 - integers
long = 8 bytes - 64 bits = -922337203685477808 to 922337203685477807 - integers
float = 4 bytes - 32 bits = approximately 3.40282347E+38 = Floating point
double = 8bytes - 64 bits = 1.79769313486231570W+308 = Floating Point
Chart = 16-bit Unicode characters = 0 to 65536 = characters
Boolean = Have true and false = boolean values
I tested our codes with 78 terms and see the difference (use scroll and go to the end):
ex10
Digite a quantidade de termos
78
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 -1323752223 512559680 -811192543 -298632863 -1109825406 -1408458269 1776683621 368225352 2144908973 -1781832971 363076002 -1418756969 -1055680967 1820529360 764848393 -1709589543 -944741150 1640636603 695895453 -1958435240 -1262539787 1073992269 -188547518 885444751 696897233 1582341984 -2015728079 -433386095 1845853122 1412467027 -1036647147 375819880 Fim
CONSTRUÍDO COM SUCESSO (tempo total: 3 segundos)
my Fibonacci
Digite a quantidade de termos: 78
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757
CONSTRUÍDO COM SUCESSO (tempo total: 3 segundos)
Your program only doesn’t work perfect when you ask for "1" as it would return two numbers. Otherwise, it is perfect
– Jefferson Quesado