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;
		
		String menu="1 - Factorial"+
						"\n2 - Summation";
						
		
		MathStuff mathObj= new MathStuff();
		Scanner scn=new Scanner(System.in);
		do
		{
			System.out.println(menu);
			System.out.println("Please select a number of the operation you want to perform: ");
			do
			{
				selection=scn.nextInt();
				success=mathObj.setMenu(selection);
				if(!success)
				{
					System.out.println("Please enter a number that matches the menu");
				}
			}while(!success);
			
		
				switch(selection)
				{
					case 1:System.out.print("Please enter a whole number: ");
							 int facNum=scn.nextInt();
							 mathObj.setFactorialNumber(facNum);
							 mathObj.calcFactorial();
							 System.out.println("The answer is "+mathObj.getFactorialNumber()+".");
							 break;
					
					case 2: System.out.print("Please enter a whole number: ");
							 int sumNum=scn.nextInt();
							 mathObj.setSummationNumber(sumNum);
							 mathObj.calcSummation();
							 System.out.println("The answer is "+mathObj.getSummationNumber()+".");
							 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);
		}

	}

}

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

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

This article has been dead for over six months. Start a new discussion instead.