Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Array and for loop

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Array and for loop

    Could anyone help me figure out why is the value inside double [] storagePI is adding onto each other? I want each to have it own value each time the outer for loop run but it is adding the previous value in the array to the next one too.

    HTML Code:
    import java.util.Scanner;
    import java.util.Random;
    public class DartsTEST
    {
        public static void main(String[]args)
        {   //Variables
            Scanner numTrial = new Scanner(System.in);
            Scanner numDart = new Scanner(System.in);
            System.out.print("Please enter the amount of trials: ");
            int trial = numTrial.nextInt();
    
            System.out.println();
            System.out.print("Please enter the amount of dart per trials: ");
            int dart = numDart.nextInt();
            Random randNumX = new Random();
            Random randNumY = new Random();
            double estimatePI ;
            int hits = 0 ;
            double [] storagePI = new double[trial];
            for(int counter = 0 ; counter < trial; counter++)
            {
                for(int count = 0 ; count < dart ; count++)
                {
                    double xValue = (randNumX.nextDouble()*2)-1;
                    double yValue = (randNumY.nextDouble()*2)-1;
                    if((Math.pow(xValue,2))+(Math.pow(yValue,2)) <= 1)
                    {
                        hits++;
                    }
                }
                double amountThrow = dart;
                estimatePI =(4.0 * (hits/amountThrow));
                storagePI[counter] = estimatePI; 
                System.out.println(storagePI[counter])
            }   
            System.out.println(storagePI[2]);
        }
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array and for loop

    Can you post output that shows what is happening?

    NOTE: The posted code does NOT compile without errors!
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Array and for loop

    Please enter the amount of trials: 5

    Please enter the amount of dart per trials: 1000
    3.064
    6.196
    9.34
    12.48
    15.62
    Value of storagePI at index 2 is 9.34
    - As you can see it seem like the value are adding onto each other.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array and for loop

    What should the values be that are printed?

    Is there a counter that is not being reset?
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    maple1100 (January 5th, 2013)

  6. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Array and for loop

    Oh, the value of "hits" is not being reset so it just keep adding thus it seem like the value are adding each other. Thanks you! Also, is the proper way to reset a counter is to initialize the value to 0 after it been used once?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array and for loop

    way to reset a counter is to initialize the value to 0
    Yes.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    maple1100 (January 5th, 2013)

Similar Threads

  1. Array and loop
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 16th, 2012, 04:04 AM
  2. Array and Loop?
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 13th, 2012, 02:23 PM
  3. Array and Loop?
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 13th, 2012, 04:58 AM
  4. Loop through a 2d array of objects
    By ssjg0ten5 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 28th, 2012, 09:53 PM
  5. For loop in array
    By Mickeydus in forum Loops & Control Statements
    Replies: 2
    Last Post: March 26th, 2012, 02:37 PM