Click to See Complete Forum and Search --> : Java Issues!...again can you help
gandalf6
May 20th, 2007, 05:52 PM
Im writing a code to give the factorial of a user entered number, that part works fine my problems are this:
whenever the code loops around to enter another number, teh variable keeps the value of the last number, how do i rest it so its back to 0 when the user starts again?
i want to add a section where it ask you if you would like to try again, y for yes, and q for enter, but i cant seem to get teh reader to pick up those;
and lastly why is it after they enter a negative number, it ask you for a positive, but it gives you the factorial of teh negative number anyways? thanks for your help in advance.
gandalf6
May 20th, 2007, 05:58 PM
I suppose i should put what i have so far ha ha ha
/**
* <p>Title: </p>
*
* <p>Description: A program used to find the factorial of a user entered number
* </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company:Blah Blah Blah </p>
*
* @author
* @version 1.0
*/
import java.io.*; // this has the BufferedReader class
import java.io.IOException;
public class Factorial
{
public static void main(String args[]) throws IOException {
int UserEnt; //number of times executed.
int answer = 1; // holds the value calculated.
// Prompt the user to enter a positive number.
{
{
do
{
System.out.println("Please enter a Positive Number");
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
InputStreamReader input = new InputStreamReader(System.in);
UserEnt = Integer.parseInt(console.readLine());
// calculating the factorial
if(UserEnt < 0)
System.out.println("Ooopps...I said a POSITIVE number...try again please.");
else;
for (int i = 1; i <= UserEnt; i++)
answer = i * answer;
//Printing out the factorial answer.
System.out.println("The factorial of " + UserEnt + " is " +
answer);
}
while(UserEnt >0);
System.out.println("Try again? y for yes, q for quit");
String string = "";
string = InputStreamReader.readLine();
}
}
}
}
petes1234
May 20th, 2007, 09:54 PM
Couple of quick questions and comments:
Why do you have an extra set of { } with no purpose?
Why do you have an InputStreamReader declared twice? The one declared anonymously within the bufferedreader is enough.
You end your loop with "string = InputStreamReader.readLine();". You have a console class for input, and InputStreamReader doesn't have a readLine() method.
You declare your console bufferedreader variable inside a loop where it won't be visible to any part of the program outside of the loop. Perhaps you should declare it somewhere else? possibly before the loops? This may be why you can't use console to get your input for the "string" variable?
If you want your "answer" variable initialized to 1 each time the loop is run, then what do you think you need to do to achieve this? This shouldn't be hard to answer.
Finally, please use code tags. They make your code much easier to read, and when the code is easier to read, more folks are likely to read it.
Good luck!
petes1234
May 20th, 2007, 10:00 PM
Have a look at this code (also notice how the code tags improve readability):
if (UserEnt < 0)
System.out
.println("Ooopps...I said a POSITIVE number...try again please.");
else
;
for (int i = 1; i <= UserEnt; i++)
answer = i * answer;
//Printing out the factorial answer.
System.out.println("The factorial of " + UserEnt + " is "
+ answer);
If a negative number is entered, what happens? Will the for loop be entered? One way to prevent problems like this is to always enclose if and else loops with curly braces.
In other words always do this even if it is a single line statement after the if and else statements:
if (UserEnt < 0)
{
//... code that's called if "if" statement is true
}
else
{
//... code that's called if "if" statement is false
}
//... code that will run regardless of whether if statement true or false
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.