954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Factorials in Java

For a school project, I am trying to make a program that gets user input and finds the factorial of the input. I wrote this bit of code and got tons of errors. Help, anybody?

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
 
class TestFactorial {
                public int x = 6;
                public int z = 0;
                public boolean xiszero = false;
                for (public int x > 0) {
                        z = (x*x-1);
                        x--;
                }
                for (public int x = 0) {
                        public boolean xiszero = true;
                        break;
                }
                for (public boolean xiszero = true) {
                        System.out.println(z)
                }
}


Errors:
Main.java:10: illegal start of type
for (public int x > 0) {
^
Main.java:10: modifier public not allowed here
for (public int x > 0) {
^
Main.java:10: ')' expected
for (public int x > 0) {
^
Main.java:10: illegal start of type
for (public int x > 0) {
^
Main.java:10: expected
for (public int x > 0) {
^
Main.java:10: ';' expected
for (public int x > 0) {
^
Main.java:11: expected
z = (x*x-1);
^
Main.java:12: expected
x--;
^
Main.java:14: class, interface, or enum expected
for (public int x = 0) {
^
Main.java:14: class, interface, or enum expected
for (public int x = 0) {
^
Main.java:15: class, interface, or enum expected
public boolean xiszero = true;
^
Main.java:16: class, interface, or enum expected
break;
^
Main.java:17: class, interface, or enum expected
}
^
Main.java:18: class, interface, or enum expected
for (public boolean xiszero = true) {
^

CodeAdmiral
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

You need a

public static void main(String args[])


in your code or a method name to encapsulate that code.

This question comes up a lot on DaniWeb: http://www.daniweb.com/search.php?q=factorial

...and...

http://www.daniweb.com/software-development/java/threads/393559

thines01
Postaholic
Team Colleague
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Where in my code would that line go?

CodeAdmiral
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Do you have a HelloWorld program set aside you can reference?
Once you see it, the solution will be clear.

thines01
Postaholic
Team Colleague
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: