2016-12-09 65 views
-2

我想在if語句(我寫的+ i +)中使用「int i」。Java中的變量內部變量

下面的代碼:

for (int i = 0; i <6; i++) { 
if (mouseX > mindmap.c+i+mX - mindmap.c+i+.mD/2 && mouseX < mindmap.c+i+.mX + mindmap.c+i+.mD/2) { 
    if (mouseY > mindmap.c1.mY - mindmap.c1.mD/2 && mouseY < mindmap.c1.mY + mindmap.c1.mD/2) { 
    println("hej"); 
    } 
} 

我希望我的循環中使用的變量mindmap.c0.mX在循環的第一次迭代,即mindmap.c1.mX在第二次迭代後,再mindmap.c2.mX等。這可以做到嗎?

我們使用Processing作爲工具。

+0

是的,它可以做到!你的代碼不工作? –

+1

爲什麼不測試它並看看?但是,是的,這是有效的用法。 – Michael

+0

什麼是「思維導圖」? –

回答

0

局部變量的作用域位於從定義它的行定義的塊中。

... // i not visible 
{ // Start of block 

    ... 
    // i not visible 
    int i = 0; // Definition of variable i visible from here 
    .... 

    // Last line where is possible to use the variable i 
} // End of block 
// i not visible 

在你的情況下,可能在整個for循環中使用變量i。

代碼中的問題與i變量的可見性無關,但它在語法上不正確。

.mD.mX是不正確的變量名稱。這是問題。可能你只需要寫出mDmX