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]); } }


LinkBack URL
About LinkBacks
Reply With Quote
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?