2

The numThrows Variable is inciting an error of variable not found when used in the main method. even though i declare it in one of the methods. I use the declare the variable in the void prompt method. This program is designed to calculate Pi using random coordinates then uses a formula to estimate pie over a user given amount of tries.

import java.util.Random;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.IOException;
public class Darts




 public static void prompt()
 {
     Scanner in = new Scanner(System.in);
    System.out.println("How many throws per trial would you like to do?: ");
    int numThrows = in.nextInt();
    System.out.println("How many trials would you like to do?: ");
    int numTrials = in.nextInt(); 



    }


 public static double[] randomX( int numThrows)
 {
     int darts = 0;
     int i = 0;
     double[] cordX = new double[numThrows];
     while(darts <= numThrows)
     {
         cordX[i] = Math.random();
         i++;
        }
     return cordX;
    }
 public static double[]randomY(int numThrows)
 {
     int darts = 0;
     int i = 0;
     double [] cordY = new double[numThrows];
     while(darts <= numThrows)
     {
         cordY[i] = Math.random();
         i++;
        }
     return cordY;


    }
 public static void getHits(int numThrows, double[] cordX, double[] cordY)
 {
     int ii = 0;
     int i = 0;
     double hits = 0;
     double misses = 0;
     for(i = 0; i <= numThrows; i++)
      {
     if( Math.pow(cordX[ii],2) + Math.pow(cordY[ii],2) <= 1)
     {
         hits++;
         ii++;

        }
        else{
            misses++;
        }
    }

    }
 public static double calcPi(int misses, int hits)
{
    int total = hits + misses;
    double pi = 4 * (hits / total);

}
// public static void print(double pi, int numThrows)
// {

   //  System.out.printf("  %-7s         %3.1f            %7s\n", "Trial[//numtrial]: pi = "

 //   }


   public static void main(String[] args)throws IOException
    {
     prompt();
     double[] cordX = randomX(numThrows);
     double[] cordY = randomY(numThrows);
     gethits();
     double pi = calcPi(misses, hits);

}

}

3 Answers3

2

If numThrows is declared within another function, then its scope does not extend to the main method.

Instead, if you want to use it in both the main method and the other one, make it a class instance.

For example:

class SomeClass {
    public static int numThrows = 2;
    public static void test() {
        numThrows = 4; // it works!
    }
    public static void main(String[] args) {
        System.out.println(numThrows); // it works!
    }
}

Therefore, its scope will be extended to all the members of the class, not just the method.

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
1

numThrows is an instance variable to your prompt method. If you want to do what I think you want to do, make numThrows a static variable outside any methods.

It will look like this:

public class Darts {
    public static int numThrows
    public static int numTrials

These variables can be referenced from any method. This should fix it.

Chris
  • 566
  • 2
  • 7
  • 22
1

Try to remove the method prompt() it's unused, and put his block in the main method.

 public static void main(String[] args)throws IOException
  {
   Scanner in = new Scanner(System.in);
    System.out.println("How many throws per trial would you like to do?: ");
    int numThrows = in.nextInt();
    System.out.println("How many trials would you like to do?: ");
    int numTrials = in.nextInt(); 
    double[] cordX = randomX(numThrows);
    ...
Abdelhak
  • 8,299
  • 4
  • 22
  • 36