It looks like you're having difficulties with scope.
If you declare variables inside a block, like you do in the case of if (something) { double x = ...; }
, the variables are visible only inside the declared block.
If you later want to access these variables, you have to change the scope so that they are visible in the block you want to use them in. Of course you could just declare them at the global level, but the art lies in finding the smallest effective scope.