import java.util.Scanner; class MCBin { public static void main(String[] args) { Scanner sc=new Scanner(System.in); //********************************************************************** // * // Get the number of histories desired * // * //********************************************************************** while(true) { System.out.println("\n Number of bins?"); int nbin=sc.nextInt(); System.out.println("\n Lower limit of bins?"); double a=sc.nextDouble(); System.out.println("\n Upper limit of bins?"); double b=sc.nextDouble(); double dx=(b-a)/(double)nbin; int n=0; System.out.println("\n Number of histories?"); int ntry=sc.nextInt(); if(ntry < 0)System.exit(0); if(ntry >0)n=ntry; //********************************************************************** // * // Initialize the two bins we will be collecting information in. * // * //********************************************************************** double[] total_score=new double[nbin]; double[] total_squared=new double[nbin]; //********************************************************************** // * // For each history: * // * //********************************************************************** for(int ihistory=0;ihistory-1 && ibin < nbin) { total_score[ibin]+=1.; total_squared[ibin]+=1.*1.; } } //********************************************************************** // * // After all histories have been run: * // * // Find the estimate of the average * // * //********************************************************************** for(int i=0;i %4$10.5f +/- %5$10.5f (%6$6.3f percent)\n", (i+1),i*dx,(i+1)*dx,xhat*nbin,mean_sd*nbin,mean_sd/xhat*100.); } } } //********************************************************************** //********************************************************************** static double getX() { double x=Math.random(); return Math.sqrt(x); } }