Results 1 to 3 of 3
Thread: "else without if"
- 10-28-2008, 10:36 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
"else without if"
import java.util.*;
public class Factorials
{
public static void main (Strings[] args)
{//Use if and while statements to calculate factorials
// Declaring of variable
int Factorials;
int nextFactorial=1;
int counter;
counter = 1;
Scanner myScanner = new Scanner(System.in);//Allows user input
//to enter an integer greater than 0
Factorials = myScanner.nextInt();//Reads the input
while (Factorials < 0)
{System.outprintln("The factorial of 0 is 1");//Prints if integer entered is 0
else{
while (counter <= Factorials)
{nextFactorial *=counter;//Computers the factorial starting at 1 and counting
up
counter++;}//increment operator
System.out.println("The factorial of " + Factorials +" is: "+
nextFactorial);
//Prints out the factorial of the entered positive integer
}
}
}
I have no idea how to fix it
- 10-28-2008, 10:51 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you explain what's you question is much clearly, I don't think know once wants to run your code and fix all bugs there. I can see lots of basis misses there in your code.
- 10-28-2008, 11:01 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Here is a very bad way. Just think why is that.
Java Code:public static void main (String[] args) { int number; // Holds the user input int result = 1; // Result Scanner input = new Scanner(System.in); // Scanner object to get user input System.out.println("Enter the number: "); number = input.nextInt(); for(int i = 1; i <= number; i++) { result *= i; System.out.println(i + "! = " + result); } }
Similar Threads
-
Hwlp with "Open", "Save", "Save as..."
By trill in forum New To JavaReplies: 3Last Post: 11-02-2010, 09:26 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
<core:forEach var="" begin="+<%=j%>+">???
By freddieMaize in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-27-2008, 01:20 AM -
[SOLVED] Exception in thread "main" java.util.NoSuchElementException
By thevoice in forum New To JavaReplies: 5Last Post: 05-14-2008, 01:43 PM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks