Advertisement
Raul_julian

Fibonacci.java

Jan 27th, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.71 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Fibonacci {
  4.  
  5.     public static void main(String[] args) {
  6.        
  7.             int off = 0, soma = 0, somaAnt = 0;
  8.             String seq = "";
  9.             String limite = JOptionPane.showInputDialog("Insira o limite");
  10.             int limiteInt = Integer.parseInt(limite);
  11.            
  12.             for(int i=1; i<limiteInt; i++) {    
  13.                    
  14.                     if(i == 1) {
  15.                         somaAnt = 0;
  16.                         seq += somaAnt + ", ";
  17.                         continue;
  18.                     }  
  19.                    
  20.                     if(i == 2) {
  21.                         soma = 1;
  22.                         seq += soma + ", ";
  23.                         continue;
  24.                     }
  25.                    
  26.                     off = soma + somaAnt;
  27.                     somaAnt = soma;
  28.                     soma = off;
  29.                    
  30.                     seq += off + ", ";
  31.                        
  32.                
  33.             }
  34.        
  35.             JOptionPane.showMessageDialog(null, seq);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement