The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

dd

Posted by ddd on November 15, 2000 at 9:53 PM

> example code:
> public class HelloWorld {
> public static void main(String args[]) {
> System.out.println("Hello World");
> System.out.println("args.length: " + args.length);
>
> int factorial = 1;
> int x = 0;
> for (int i = args.length; i > 0; i--) {
> factorial = factorial * i;
> System.out.println("args" + x + ": " + args[x]);
> x++;
> }
>
> System.out.println("factorial: " + factorial);
> }
> }

> > I've never programmed before in Java, actually I've never programmed, period. I'm attempting to teach myself. I was looking at the HelloWorld program below:

> > // A simple program to print out the string "HelloWorld".

> > public class HelloWorld
> > {

> > /* Applications always execute the main method of
> > the class first.

> > @param args[] contains the command line arguments.
> > */

> > public static void main(String args[])
> > {
> > System.out.println("Hello world!");
> > }

> > }

> > Suppose I wanted the program to echo the number of runtime arguments. For instance, if I type java HelloWorld this is a test
> > the output should be 4 because there are four arguments passed to the program. (The name of the program is HelloWorld)

> > What if I wanted the program to echo the factorial of the number of arguments to the program. For instance, if I type java HelloWorld this is a test the output should be 24 (= 4 x 3x 2 x 1), which is the factorial of 4.

> > What changes would I have to make to the above code. I tried the following but it gives a blank answer:

> > /*A simple program to echo the number of runtime
> > arguments*/

> > public class HelloWorld
> > {
> > public static void main (String args[])
> > {
> > for (int i = 0; i < args.length; i++)
> > {
> > System.out.print(args[i]+" ");
> > }
> > System.out.println(" ");
> > }
> > }

> >
> > Thanks in advance.






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us