2

numThrows 変数は、メイン メソッドで使用されたときに、変数が見つからないというエラーを引き起こしています。メソッドの1つで宣言していますが。void プロンプト メソッドで変数の宣言を使用します。このプログラムは、ランダムな座標を使用して Pi を計算するように設計されており、数式を使用して、ユーザーが指定した試行回数で円を推定します。

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

}

}
4

3 に答える 3

2

numThrowsが別の関数内で宣言されている場合、そのスコープはメイン メソッドには拡張されません。

代わりに、mainメソッドと他のメソッドの両方で使用する場合は、クラス インスタンスにします。

例えば:

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!
    }
}

したがって、そのスコープは、メソッドだけでなく、クラスのすべてのメンバーに拡張されます。

于 2016-01-09T00:25:31.827 に答える
1

numThrows は、prompt メソッドのインスタンス変数です。あなたがやりたいと思うことをしたい場合は、numThrowsをメソッドの外で静的変数にしてください。

次のようになります。

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

これらの変数は、どのメソッドからも参照できます。これで修正されるはずです。

于 2016-01-09T00:24:42.083 に答える
1

使用されていないメソッドを削除して、prompt()そのブロックをメイン メソッドに配置してみてください。

 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);
    ...
于 2016-01-09T00:26:47.990 に答える