Java Error'错误:找不到符号'

时间:2015-02-09 05:05:24

标签: java

我正在学习Java。以下是我一直在努力编写的程序,但无法弄清楚为什么' x'在第38行中提供了以下错误:'找不到符号'。任何帮助将不胜感激。

import java.util.Scanner;

class metropolis_HW2_7 {
    static int count = 0;

    public static void main(String[] args) {
        double a = 0.;
        double b = Math.PI;
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println(" Number of bins?");
            int nbin = sc.nextInt();
            if (nbin < 1)
                System.exit(0);
            double[] bin = new double[nbin];
            System.out.println(" Number of histories to run?");
            int N = sc.nextInt();
            double dx = (b - a) / nbin;
            for (int i = 0; i < N; i++) {
                if (count == 0) {
                    double squiggle1 = Math.PI * Math.random();
                    double squiggle2 = Math.PI * Math.random();
                    double y_1 = 2 * squiggle1 + Math.sin(squiggle1);
                    double y_2 = 2 * squiggle2 + Math.sin(squiggle2);
                    if (y_2 < y_1) {
                        squiggle1 = squiggle2;
                        double x = squiggle2;
                    } else {
                        squiggle1 = squiggle1;
                        double x = squiggle2 / squiggle1;
                    }
                    count++;
                } else {
                    double squiggle1;
                    double x = Sample(squiggle1);
                }
                int binNumber = (int) ((x - a) / dx);
                bin[binNumber] += 1.;
            }
            double x = a - dx / 2.;
            for (int i = 0; i < nbin; i++) {
                x += dx;
                bin[i] /= N * dx;
                System.out.printf(" Bin %1$5d Sample for x = %2$7.5f is %3$7.5f vs %4$7.5f Ratio (%5$f) \n", i, x, bin[i], PDF(x), bin[i] / PDF(x));
            }
        }
    }

    static double Sample(double squiggle1) {
        double squiggle2 = Math.PI * Math.random();
        double y_1 = 2 * squiggle1 + Math.sin(squiggle1);
        double y_2 = 2 * squiggle2 + Math.sin(squiggle2);
        if (y_2 < y_1) {
            squiggle1 = squiggle2;
            return squiggle2;
        } else {
            squiggle1 = squiggle1;
            return squiggle2 / squiggle1;
        }
        count++;
    }

    static double PDF(double x) {
        return (2 * x + Math.sin(x)) / (Math.pow(Math.PI, 2) + 2);
    }
}

3 个答案:

答案 0 :(得分:2)

变量只存在于声明范围内({}之间)。你有三个不同的变量叫x,当行{时,它们都不存在{1}}已执行。

int binNumber=(int)((x-a)/dx);语句之外声明变量 ,然后将其分配到其中,如下所示:(我删除了大部分代码以使此示例更清晰;显而易见仍然需要它)

if

答案 1 :(得分:1)

全局声明double x变量。你在else部分声明了为什么它找不到变量。

范围变量示例:

    int a = 80;  // Create a global variable "a"

void setup() {
  size(640, 360);
  background(0);
  stroke(255);
  noLoop();
}

void draw() {
  // Draw a line using the global variable "a"
  line(a, 0, a, height);

  // Create a new variable "a" local to the for() statement 
  for (int a = 120; a < 200; a += 2) {
    line(a, 0, a, height);
  }

  // Create a new variable "a" local to the draw() function
  int a = 300;
  // Draw a line using the new local variable "a"
  line(a, 0, a, height);  

  // Make a call to the custom function drawAnotherLine()
  drawAnotherLine();

  // Make a call to the custom function setYetAnotherLine()
  drawYetAnotherLine();
}

void drawAnotherLine() {
  // Create a new variable "a" local to this method
  int a = 320;
  // Draw a line using the local variable "a"
  line(a, 0, a, height);
}

void drawYetAnotherLine() {
  // Because no new local variable "a" is set, 
  // this line draws using the original global
  // variable "a", which is set to the value 80.
  line(a+2, 0, a+2, height);
}

答案 2 :(得分:0)

变量x未在该行使用的范围内声明。您正在两个不同的double内定义并分配if-blocks x。尝试在更广泛的范围内声明变量(例如,在if-block之前,然后在本地分配它。然后它将在所有3个位置都可访问。

这里有一个简单的例子来解释我的意思:

void method()
{
    if (2 > 1)
        double x = 2;
    else
        double x = 3;

     System.out.println(x); //ERROR, because x is out of scope
}

所以将其改为

void method()
{
    double x = 0;
    if (2 > 1)
        x = 2;
    else
        x = 3;

     System.out.println(x); //No error; x is referenced in the same scope in which it is declared
}