User/Event Loop issue

yes/no continue? Does not continue...

Page 1 of 1

3 Replies - 1400 Views - Last Post: 07 March 2008 - 01:36 AM Rate Topic: -----

#1 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

User/Event Loop issue

Posted 07 March 2008 - 12:17 AM

I'm just not sure what the problem is with my program, all appears well with my user loop where the user is asked to enter yes or no to continue. But when I type yes, it only loops back through the println statements but does not go through the calculations.
import java.util.Scanner;		// e is approx. 2.718281828459

public class Factorial {
	
	public static void main(String[] args)
	{
		int x;
		double sum = 1;
		double n = 0;
		double count = 1;
//		======
		boolean play=false;
		String playAgain;
//		=================
		do{
		Scanner keyboard = new Scanner(System.in);	
		System.out.println("This program will take an input number as 'x' and output the factorial");
		System.out.println("of e^x for 'n' taken to be each of the values 1 to 10, 50, and 100.");
		System.out.println("====================================================================");
		System.out.println("Please enter an integer value:");
		x = keyboard.nextInt();
		System.out.println(x+" will now be calculated as the factorial of e^x....");
		System.out.println("========================================================");
//		=======================
		for (int ii = 1; ii <= 100; ii++)
		{
			n++;
			count = count*n;
			if ((n <= 10)||(n == 50)||(n == 100))
			{
				sum = sum + (Math.pow(x, n)/count);
				System.out.println("When 'n' = "+ii+" then 'x' = "+sum);
			}// if
		
		}// for loop
//		====================================
		Scanner in = new Scanner(System.in);
		System.out.println();
		System.out.println("========================================");
		System.out.println("Would you like to continue (yes/no)?");
		System.out.println("========================================");
		playAgain = in.nextLine();
		if(playAgain.equals("yes"))
		{
			play = true;
		}
			if(playAgain.equals("no"))
			{
				play = false;
			}
		}while(play==true);
	

		}// main
//		========

 	}// class Factorial



The output after typing 'yes':
This program will take an input number as 'x' and output the factorial
of e^x for 'n' taken to be each of the values 1 to 10, 50, and 100.
====================================================================
Please enter an integer value:
1
1 will now be calculated as the factorial of e^x....
========================================================
When 'n' = 1 then 'x' = 2.0
When 'n' = 2 then 'x' = 2.5
When 'n' = 3 then 'x' = 2.6666666666666665
When 'n' = 4 then 'x' = 2.708333333333333
When 'n' = 5 then 'x' = 2.7166666666666663
When 'n' = 6 then 'x' = 2.7180555555555554
When 'n' = 7 then 'x' = 2.7182539682539684
When 'n' = 8 then 'x' = 2.71827876984127
When 'n' = 9 then 'x' = 2.7182815255731922
When 'n' = 10 then 'x' = 2.7182818011463845
When 'n' = 50 then 'x' = 2.7182818011463845
When 'n' = 100 then 'x' = 2.7182818011463845

========================================
Would you like to continue (yes/no)?
========================================
yes
This program will take an input number as 'x' and output the factorial
of e^x for 'n' taken to be each of the values 1 to 10, 50, and 100.
====================================================================
Please enter an integer value:
2
2 will now be calculated as the factorial of e^x....
========================================================

========================================
Would you like to continue (yes/no)?
========================================


What am I missing here? :blink:

Is This A Good Question/Topic? 0
  • +

Replies To: User/Event Loop issue

#2 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: User/Event Loop issue

Posted 07 March 2008 - 12:55 AM

ur mistake.. well its actually really simple

you need to move the calculation varialbes(sum,n,count,) inside the loop

other wise they dont reset :o try move those three into the loop and c if thts wt u want
Was This Post Helpful? 0
  • +
  • -

#3 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

Re: User/Event Loop issue

Posted 07 March 2008 - 01:11 AM

View Postgl3thr0, on 7 Mar, 2008 - 12:55 AM, said:

ur mistake.. well its actually really simple

you need to move the calculation varialbes(sum,n,count,) inside the loop

other wise they dont reset :o try move those three into the loop and c if thts wt u want

WOW! DOH! :crazy: thanks! The devil is in the details....
Was This Post Helpful? 0
  • +
  • -

#4 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: User/Event Loop issue

Posted 07 March 2008 - 01:36 AM

haha np
you might want to look into breaking your program into methods.. as all that code in the main makes it a little hard to understand.. and really nt very pretty :P

for example u keep using the line
System.out.println("====================================================================");

and it seperates your code and its kind hard to use so heres a sample method

public void makeLine(){
	 for(int i=0;i<20;i++){
		   System.out.print("=");
	 }
}
//now in the main method
public static void main( ......){
	 Factorial f = new Factorial();
	 f.makeLine();
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1