Methods 找不到符号-变量。在主方法中,当符号用于其他方法时
该程序用于通过随机抛出次数(x,y坐标)计算Pi,然后对整个过程进行多次迭代。Methods 找不到符号-变量。在主方法中,当符号用于其他方法时,methods,main-method,cannot-find-symbol,Methods,Main Method,Cannot Find Symbol,该程序用于通过随机抛出次数(x,y坐标)计算Pi,然后对整个过程进行多次迭代。 我在代码的最后一行double average=average(pi)上得到一个错误,它说找不到符号变量pi,即使我使用它并在average方法中声明它 /* @author @version January 8th 2016 */ import java.util.Scanner; import java.util.Random; import java.util.Random; import java.uti
我在代码的最后一行double average=average(pi)上得到一个错误,它说找不到符号变量pi,即使我使用它并在average方法中声明它
/*
@author
@version January 8th 2016
*/
import java.util.Scanner;
import java.util.Random;
import java.util.Random;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.IOException;
public class Darts2 {
public static int numThrows;
public static int numTrials;
public static int misses;
public static int hits;
public static void prompt() {
Scanner in = new Scanner(System.in);
System.out.println("How many throws per trial would you like to do?: ");
numThrows = in.nextInt();
System.out.println("How many trials would you like to do?: ");
numTrials = in.nextInt();
}
public static double[] randomX( int numThrows){
int darts = 0;
int i = 0;
double[] cordX = new double[numThrows];
while(darts < numThrows - 1) {
cordX[i] = Math.random();
i++;
darts++;
}
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++;
darts++;
}
return cordY;
}
public static void getHits(int numThrows, double[] cordX, double[] cordY) {
int ii = 0;
int i = 0;
hits = 0;
misses = 0;
for(i = 0; i < numThrows; i++) {
if( Math.pow(cordX[ii],2) + Math.pow(cordY[ii],2) <= 1) {
hits++;
} else{
misses++;
}
ii++;
}
}
public static double calcPi(int misses, int hits) {
int total = hits + misses;
double pi = 4 * ((double)hits / total);
return pi;
}
public static void print(double pi, int numThrows) {
System.out.printf("\nThe pi estimate for this trial is: " + pi);
}
public double average(double[] pi) {
double sum = 0;
double average;
for(int i = 0; i < pi.length; i++) {
sum = sum + pi[i];
}
average = (double)sum/pi.length;
return average;
}
public static void main(String[] args)throws IOException {
prompt();
int x = 0;
for(x = 0; x < numTrials; x++) {
double[] cordX = randomX(numThrows);
double[] cordY = randomY(numThrows);
getHits(numThrows, cordX, cordY);
double pi = calcPi(misses, hits);
print(pi, numThrows);
}
double average = average(pi);
}
}
/*
@作者
@2016年1月8日版本
*/
导入java.util.Scanner;
导入java.util.Random;
导入java.util.Random;
导入java.util.Scanner;
导入java.io.PrintWriter;
导入java.io.File;
导入java.io.IOException;
公共类飞镖2{
公共静态整数;
公共静态整数;
公共静态int未命中;
公共静态整数点击;
公共静态void提示符(){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“您希望每次试验进行多少次投掷?:”;
numThrows=in.nextInt();
System.out.println(“您希望进行多少次试验?:”;
numTrials=in.nextInt();
}
公共静态双[]随机数x(整数numThrows){
int-darts=0;
int i=0;
double[]cordX=新的double[numThrows];
while(省道pi变量,因此其范围仅限于for循环
要解决此问题,请在for循环之前初始化pi变量,如下所示:
public static void main(String[] args)throws IOException
{
prompt();
double pi = 0;
for(int x = 0; x < numTrials; x++)
{
double[] cordX = randomX(numThrows);
double[] cordY = randomY(numThrows);
getHits(numThrows, cordX, cordY);
pi = calcPi(misses, hits);
print(pi, numThrows);
}
double average = average(pi);
}
publicstaticvoidmain(字符串[]args)引发IOException
{
提示();
双pi=0;
对于(int x=0;x