Advertisement
HasteBin0

Java Fibonacci Exercise

Jun 25th, 2017
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. package fibonacci;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  *
  7.  * @author Aidan
  8.  */
  9. public class Fibonacci {
  10.  
  11.     static {
  12.         renew();
  13.     }
  14.  
  15.     private static void renew() {
  16.         input = new Scanner(System.in);
  17.     }
  18.  
  19.     private static Scanner input;
  20.  
  21.     public static void main(String[] args) {
  22.         Integer a = 0, b = 1, c = 0, upper;
  23.  
  24.         do {
  25.             System.out.print("Enter the number to approch: ");
  26.             try {
  27.                 upper = input.nextInt();
  28.             } catch (Exception e) {
  29.                 upper = 0;
  30.             }
  31.             renew();
  32.         } while (upper <= 0);
  33.  
  34.         do {
  35.             c = a + b;
  36.             a = b;
  37.             b = c;
  38.             //
  39.             System.out.print(a + " ");
  40.             System.out.flush();
  41.         } while (c < upper);
  42.        
  43.         System.out.println("\nReached " + a + '.');
  44.         System.out.flush();
  45.  
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement