2015-02-09 69 views
2

我正在学习Java的过程。下面是我一直试图编译的程序,但无法弄清楚为什么第38行中的'x'提供了以下错误:'找不到符号'。任何帮助将不胜感激。Java错误'error:can not find symbol'

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); 
    } 
} 
+0

Crossposted在http://www.coderanch.com/t/645832/java/java/Java-Assignment-School-error-find – maneesh 2015-02-09 06:17:16

回答

2

变量只范围内有他们在,你有三个不同的变量称为x声明,并没有O({}之间)当行int binNumber=(int)((x-a)/dx);被执行时,它们存在。

声明一个变量if语句,然后分配给它里面,是这样的:(我已经去除了大部分代码,以使这个例子更清晰;很明显,你仍然需要它)

double x; 
if (count==0) { 
    if (y_2<y_1) { 
     x=squiggle2; 
    } else { 
     x=squiggle2/squiggle1; 
    } 
} else { 
    x=Sample(squiggle1); 
} 
int binNumber=(int)((x-a)/dx); 
0

变量x未在该行所使用的范围内声明。您在两个不同的if-blocks内定义并分配double 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 
} 
1

在全局声明double x变量。在其他部分声明它为什么找不到变量。

Scope变量示例:

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); 
} 
相关问题