Hello,
I am making a program that calculates factorial and summation. I ask the user what they would like to do, then ask them to enter a whole number. The program I wrote has loops, so if the user wants to do this again, they can.

When I do the loop the second time, and enter a whole number, it seems the program adds the new input with the old input and I get a really big number. Is there a way to clear the last input used?

Thanks.

``````char selectionYesOrNo;
boolean success;
int selection;

"\n2 - Summation";

MathStuff mathObj= new MathStuff();
Scanner scn=new Scanner(System.in);
do
{
System.out.println("Please select a number of the operation you want to perform: ");
do
{
selection=scn.nextInt();
if(!success)
{
}
}while(!success);

switch(selection)
{
case 1:System.out.print("Please enter a whole number: ");
int facNum=scn.nextInt();
mathObj.setFactorialNumber(facNum);
mathObj.calcFactorial();
break;

case 2: System.out.print("Please enter a whole number: ");
int sumNum=scn.nextInt();
mathObj.setSummationNumber(sumNum);
mathObj.calcSummation();
break;

}

System.out.print("Do you want to do this again? (y or n): ");
scn.nextLine();
selectionYesOrNo=scn.nextLine().charAt(0);

}
while((selectionYesOrNo=='y')||(selectionYesOrNo=='Y'));
if((selectionYesOrNo=='n')||(selectionYesOrNo=='N'))
{
System.exit(0);
}

}

}``````

Factorial is one argument operator, result is ready immediately. Summation - no.
You need to define the end of sum operation

set facnum = 0 at the end of the loop

Factorial is one argument operator, result is ready immediately. Summation - no.
You need to define the end of sum operation