I am in the process of learning Java. Below is the program that I've been trying to compile, but cannot figure out why 'x' in line 38 provides the following error: 'cannot find symbol'. Any help would be greatly appreciated.
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 %15ドルd Sample for x = %27ドル.5f is %37ドル.5f vs %47ドル.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);
}
}
-
Crossposted at coderanch.com/t/645832/java/java/…maneesh– maneesh2015年02月09日 06:17:16 +00:00Commented Feb 9, 2015 at 6:17
3 Answers 3
Variables only exist inside the scope (between { and }) they're declared in. You have three different variables called x, and none of them exist when the line int binNumber=(int)((x-a)/dx); is executed.
Declare a variable outside the if statements, and then assign it inside it, something like this: (I've removed most of your code to make this example clearer; obvious you still need it)
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);
Comments
Declare double x variable globally.You are declared in else part thats why it could not find the variable.
Scope variable Example:
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);
}
Comments
The variable x is not declared in the scope in which it is used at that line. You are defining and assigning the double x inside two different if-blocks. Try declaring the variable in a broader scope (say, before the if-block, then assign it locally. Then it will be accessible in all 3 places.
Here's a simple example to explain what I mean:
void method()
{
if (2 > 1)
double x = 2;
else
double x = 3;
System.out.println(x); //ERROR, because x is out of scope
}
So change it to
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
}