# need some help!

2 replies [Last post]
jdtan
Offline
Joined: 2009-11-16

Im in desperate need of help. I need to modify this code which is doing Fibonacci Numbers. I need to modify the code so that it restricts the value of the first number to a whole number greater than or equal to zero, restrict the value of the second number greater than or equal the first number, and restrict the value of the input when prompted for the element to dsiplay a whole number greater than or equal to one.

I'm really confused and need help

This is a part of the code i need to modify.

String inputString;
String outputString;

int previous1;
int previous2;
int current = 0;
int counter;
int nthFibonacci;

inputString = JOptionPane.showInputDialog("Enter the first " + "Fibonacci number; ");
previous1 = Integer.parseInt(inputString);

inputString = JOptionPane.showInputDialog("Enter the second " + "Fibonacci number; ");
previous2 = Integer.parseInt(inputString);

outputString = "The first two number sof the " + "Fibonacci sequence are: " + previous1 + " and " + previous2;

inputString = JOptionPane.showInputDialog("Enter the position " + "of the desired number in " + "the Fibonacci sequence: ");
nthFibonacci = Integer.parseInt (inputString);

if (nthFibonacci == 1)
current = previous1;
else if (
else if (nthFibonacci == 2)
current = previous2;
else
{
counter = 3;

while (counter <= nthFibonacci)
{
current = previous2 + previous1;
previous1 = previous2;
previous2 = current;
counter++;

}

}

outputString = outputString + "\nThe " + nthFibonacci + "th Fibonacci number of " + "the sequence is: " + current;

JOptionPane.showMessageDialog(null, outputString, "Fibonacci Number", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);

fardoonmuhafiz
Offline
Joined: 2009-11-04

import javax.swing.JOptionPane;
public class FibonacciNumbers {
public static void main(String[] args) {
int previous1=0,previous2=1;
int current=0;
int range;

range=Integer.parseInt(JOptionPane.showInputDialog("Enter the range of fibonacci numbers you want to display: "));
while(range<1){

range=Integer.parseInt(JOptionPane.showInputDialog("Enter the range of fibonacci numbers which is a whole number: "));
}//end of while
System.out.print("The fibonnacci numbers are "+ previous1+" "+previous2);
for(int i=2;i current=previous1+previous2;
System.out.print(" "+current);
previous1=previous2;
previous2=current;
}//end of for
}//end of main
}//end of class

fardoonmuhafiz
Offline
Joined: 2009-11-04

This is the code which prompts to enter a whole number(when you enter a negative number or zero)...hope this is the code you are looking for....

/////////////////////////////////////////////

import javax.swing.JOptionPane;

public class FibonacciNumbers {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//String inputString,outputString;
int previous1=0,previous2=1;
int current=0;
int range;

range=Integer.parseInt(JOptionPane.showInputDialog("Enter the range of fibonacci numbers you want to display: "));
while(range<1){

range=Integer.parseInt(JOptionPane.showInputDialog("Enter the range of fibonacci numbers which is a whole number: "));
}//end of while
System.out.print("The fibonnacci numbers are "+ previous1+" "+previous2);
for(int i=2;i current=previous1+previous2;
System.out.print(" "+current);
previous1=previous2;
previous2=current;
}//end of for

}//end of main

}//end of class