User input at runtime?? [message #128532] |
Mon, 09 January 2006 17:24 ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: asdad.asd.com
I have this in a java file:
public class Fibonacci{
public static void main (String[] args) {
int theNum, theFib;
System.out.println("This program computes the kth " +
"Fibonacci number.");
System.out.print("Enter the number k: ");
theNum = new Integer(args[0]).intValue();
but to get input in theNum I need to specify it under Run...Is there no way
to enter input when I run the program?
|
|
|
|
Re: User input at runtime?? [message #128558 is a reply to message #128545] |
Mon, 09 January 2006 18:10 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: asdad.asd.com
Jeff Myers wrote:
> You're reading the command line argument arg[0] as input. If you want to
> read console-based input at runtime you'll have to use System.in.read()
>
> - Jeff
I can now type something in the console but it never gets used:
import java.io.IOException;
public class Fibonacci{
public static void main (String[] args) throws IOException {
int theFib, theNum;
System.out.println("This program computes the kth " +
"Fibonacci number.");
System.out.print("Enter the number k: ");
theNum= System.in.read();
theFib = fib(theNum);
System.out.println("The " + theNum + "th Fibonacci " +
"number = " + theFib + ".");
}
/**
* Recursively calcualte the kth Fibonacci number.
*
* @param k indicates which Fibonacci number to compute.
* @return the kth Fibonacci number.
*/
static int fib(int k) {
// Base Case:
// If k <= 2 then fib(k) = 1.
if (k <= 2) {
return 1;
}
// Recursive Case:
// If k > 2 then fib(k) = fib(k-1) + fib(k-2).
else {
return fib(k-1) + fib(k-2);
}
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.09523 seconds