My assignment ask us to ask the user to enter a number and print the factorial of it,
it also ask us to not allow the user to chose any negative number or number over 25, when they do, we loop them back to scanner to re renter a number
we were also told to store the number in a long, but when I do, if the user enters a number over 20 and =<25, the answer becomes a negative causing a long overflow.
I tried changing the long to BigInteger, but when I keep getting an error on ever line i use BigInteger instead of long
boolean correctInputn= false;
while(!correctInputn)
{
long number;// declares variables for storing number
long factorial = 1;// declare variable for storing factorial
System.out.println("Enter a number between 1 and 25"); // tells user to enter number
number = scanner.nextLong();
if (number <0)
{System.out.println("Positive numbers only");// if number entered is negative
correctInputn = false; continue;} // if user enters number less than 0 loops back to code start
else if (number > 25)
{ System.out.println("Number to large to print");
correctInputn = false; continue;} // if user enters number over 25 loops back to code start
else {
// if user enter 10, counter starts at 10 and runs to two
for(long mynumber = number; mynumber >= 1; mynumber--) {
factorial = factorial*mynumber; // mynumber would contain different values and that is multiplied by value present in factorial value and stored again in factorial variable
}
System.out.println("The factorial of " number " is equal to " factorial);
break;
}
}
CodePudding user response:
BigInteger
is a reference type - like all types in java except the hardcoded list of long
, int
, double
, short
, float
, char
, byte
, and boolean
. You can invoke methods on it. The
can be used if the thing on its left's type is a String
which will invoke toString()
on them and concatenate them. That's it. They do not 'support' -, , *, etcetera. Instead, they have the methods .multiply
and .add
and friends. Note that this doesn't add a number to the object, it calculates the value and returns it, e.g. you'd write factorial = factorial.multiply(myNumber)
. You'll have to replace all operators with the appropriate method, and you'll have to turn all numbers in BigInteger
objects, using BigInteger.valueOf(20)
for example.
You can't magically prevent longs from overflowing. It's a fundamental aspect of math on 64-bit numbers: You can't store more than 2^64 different things in 64-bits for the same reason 1 1=2 - because of fundamental mathematical reasons.
CodePudding user response:
Factorial factNum;
Scanner in = new Scanner(System.in);
boolean flagCheck = false;
System.out.println("Please give a number between 0 - 25: ");
factNum = new Factorial(in.nextLong());
//check if the number is in the limits
while(!flagCheck) {
if (factNum.getVarForFact() < 0){
System.out.println("Number must be over 0. Please enter again a number between 0-25");
factNum = new Factorial(in.nextLong());
} else if (factNum.getVarForFact() > 25){
System.out.println("Number must be under 25. Please enter again a number between 0-25");
factNum = new Factorial(in.nextLong());
} else if (factNum.getVarForFact() == 0) {
factNum.setFinalFact(1);
System.out.println("The factorial of " factNum.getVarForFact() " is " factNum.getFinalFact());
flagCheck = true;
} else{
factNum.factorial();
System.out.println("The factorial of " factNum.getVarForFact() " is " factNum.getFinalFact());
flagCheck = true;
}
}
}
}THIS WAS MAIN
//Constructor
Factorial(long varForFact){
this.varForFact = varForFact;
}
//Getter
public long getVarForFact(){
return this.varForFact;
}
public long getFinalFact() {
return finalFact;
}
//Setter
public void setVarForFact(long varForFact){
this.varForFact = varForFact;
}
public void setFinalFact(long finalFact) {
this.finalFact = finalFact;
}
//factorial method, it will return the factorial
public void factorial(){
long x = this.varForFact;
for(long i=this.varForFact-1; i>=1; i--)
x *= i;
setFinalFact(x);
}
} `THIS WAS FROM CLASS FACTORIAL