I am in a intro computer class and need help on an assignment. I have to create a program that has one main method and calls another static method to figure the fibonacci # that is input by the user. I can not use a recuring solution. here is what I have. the program complies but when I try a sample I get and error
Which Fibonacci number would you like?
8
Fibonacci #Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 'd'
at java.util.Formatter.format(Formatter.java:2431)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at mpenke_Fibonacci.main(mpenke_Fibonacci.java:37)
[code]
import java.util.Scanner;
public class mpenke_Fibonacci
{
public static void main ( String args[] )
{
int fib;
Scanner input = new Scanner (System.in);
do
{
System.out.print("Which Fibonacci number would you like?\n");
fib = input.nextInt();
}
while (fib <=0 && fib >= 70);
double num = fibcalc( fib );
System.out.printf ("Fibonacci #%d is" + num);
}
public static double fibcalc( int fib )
{
if ( fib <= 2)
return fib;
double f0 = 0;
double f1 = 1;
double current = 1;
for (int i = 2; i <= 70; i++)
{
current = f0 + f1;
f0 = f1;
f1 = current;
}
return current;
}
}
[code/]
can anyone help find my problem?
6 Replies - 1272 Views - Last Post: 09 April 2010 - 10:01 PM
#1
Creating Java program calling one static method for fibonacci numbers
Posted 05 July 2009 - 12:52 PM
Replies To: Creating Java program calling one static method for fibonacci numbers
#2
Re: Creating Java program calling one static method for fibonacci numbers
Posted 05 July 2009 - 01:02 PM
Hey, fix the second code tag
Secondly, this line : System.out.printf ("Fibonacci #%d is" + num);
must change to this : System.out.printf ("Fibonacci #%d is %.2f",fib,num);
If u are using printf instead of + place a comma ( , ) .And lastly, d is for integers.Change it to %.2f or whatever precision u want.
Secondly, this line : System.out.printf ("Fibonacci #%d is" + num);
must change to this : System.out.printf ("Fibonacci #%d is %.2f",fib,num);
If u are using printf instead of + place a comma ( , ) .And lastly, d is for integers.Change it to %.2f or whatever precision u want.
This post has been edited by AbuJaFaR: 05 July 2009 - 01:05 PM
#3
Re: Creating Java program calling one static method for fibonacci numbers
Posted 05 July 2009 - 01:10 PM
Also I am only allowed one return statement!
#4
Re: Creating Java program calling one static method for fibonacci numbers
Posted 05 July 2009 - 01:15 PM
So just return current. If you are returning fib because a number less than that is not allowed, just handle you exception there instead of returning the value.
#5
Re: Creating Java program calling one static method for fibonacci numbers
Posted 05 July 2009 - 01:18 PM
huskers23, on 5 Jul, 2009 - 12:10 PM, said:
Also I am only allowed one return statement!
import java.util.Scanner;
public class mpenke_Fibonacci
{
public static void main ( String args[] )
{
int fib;
Scanner input = new Scanner (System.in);
do
{
System.out.print("Which Fibonacci number would you like?\n");
fib = input.nextInt();
}
while (fib <=0 && fib >= 70);
double num = fibcalc( fib );
System.out.printf ("Fibonacci #%d is %f",fib,num);
}
public static double fibcalc( int fib )
{
if ( fib <= 2)
return fib;
double f0 = 0;
double f1 = 1;
double current = 1;
for (int i = 2; i <= 70; i++)
{
current = f0 + f1;
f0 = f1;
f1 = current;
}
return current;
}
}
I got it to run. I tested it with the number 8 input which should out put 21. instead it output 190392490709135.00000 which is what the answer would be if I input 70. also can cannot output any decimal places.
This post has been edited by huskers23: 05 July 2009 - 01:20 PM
#6 Guest_tyler*
Re: Creating Java program calling one static method for fibonacci numbers
Posted 09 April 2010 - 04:35 PM
The above code was on the right track but should be done like this
import java.util.Scanner;
public class mpenke_Fibonacci
{
public static void main ( String args[] )
{
int fib;
Scanner input = new Scanner (System.in);
do
{
System.out.print("Which Fibonacci number would you like?\n");
fib = input.nextInt();
}
while (fib <=0 && fib >= 70);
double num = fibcalc( fib );
System.out.printf ("Fibonacci #%d is %f",fib,num);
}
public static double fibcalc( int fib )
{
//CHANGED THIS if ( fib < 2)
fib = fib;
//CHANGED THIS double f0 = 1;
//CHANGED THIS double f1 = 0;
double fib = 1;
// CHANGED THIS for (int i = 2; i <= fib; i++)
{
fib = f0 + f1;
f0 = f1;
f1 = fib;
}
return fib;
}
}
these corrections should make the program run perfectly
import java.util.Scanner;
public class mpenke_Fibonacci
{
public static void main ( String args[] )
{
int fib;
Scanner input = new Scanner (System.in);
do
{
System.out.print("Which Fibonacci number would you like?\n");
fib = input.nextInt();
}
while (fib <=0 && fib >= 70);
double num = fibcalc( fib );
System.out.printf ("Fibonacci #%d is %f",fib,num);
}
public static double fibcalc( int fib )
{
//CHANGED THIS if ( fib < 2)
fib = fib;
//CHANGED THIS double f0 = 1;
//CHANGED THIS double f1 = 0;
double fib = 1;
// CHANGED THIS for (int i = 2; i <= fib; i++)
{
fib = f0 + f1;
f0 = f1;
f1 = fib;
}
return fib;
}
}
these corrections should make the program run perfectly
#7
Re: Creating Java program calling one static method for fibonacci numbers
Posted 09 April 2010 - 10:01 PM
Thanks for helping, but:
1) This post is a year old
2) We don't just "hand out solutions here" - we help them learn
3)
1) This post is a year old
2) We don't just "hand out solutions here" - we help them learn
3)
Page 1 of 1
|
|

New Topic/Question
This topic is locked




MultiQuote








|