2014-09-24 62 views
0

不習慣這是從代碼的摘錄:java的局部變量在Eclipse

if (resultText.equalsIgnoreCase("open browser")) 
     { 
      try{ 
      Process p; //resultText=""; 
      p = Runtime.getRuntime().exec("cmd /c start chrome.exe"); 

      }catch(Exception ae){} 
     } 

在Eclipse其顯示警告如下:

The value of local variable p is not used

有什麼錯在這裏?請幫我理解這個問題。謝謝。

回答

4

這只是IDE的警告。這不是一個錯誤。它只是讓你知道p永遠不會被使用。你可以把你的代碼寫入:

if (resultText.equalsIgnoreCase("open browser")) 
{ 
    try{ 
     Runtime.getRuntime().exec("cmd /c start chrome.exe"); 
    }catch(Exception ae){} 
} 

而且它會做同樣的事情。

2

爲什麼你需要p?

Process p; //resultText=""; 
p = Runtime.getRuntime().exec("cmd /c start chrome.exe"); 

您在分配值之後未使用它。 如果使用它,警告將消失。

你的代碼應該是:

if (resultText.equalsIgnoreCase("open browser")) 
    { 
     try{ 
      Runtime.getRuntime().exec("cmd /c start chrome.exe"); 
     }catch(Exception ae){} 
    } 

當你不需要變量p可言。

2

警告告訴您,您正在爲p分配值,但從未使用過。

這是一個警告,所以它是可以忽略的。

0

您可以忽略警告。

它只是告訴你,你爲p分配了一個值,但從未使用它。基本上

if (resultText.equalsIgnoreCase("open browser")) 
    { 
     try{ 
      Runtime.getRuntime().exec("cmd /c start chrome.exe"); 

     }catch(Exception ae){ 
      System.out.println("An error has oucurd... :d sorry"); 
     } 
    } 
+0

我不會說忽略他們,他們在那裏是爲了一個目的。忽略它們並不一定會傷害,但它們可能是有用的 – Michael 2014-09-24 17:51:18

+0

我的意思是*可以*忽略,因爲你的程序仍然會編譯並運行 – cppprog 2014-09-24 17:52:06

0

試試這個。這是你的Eclipse,警告你,你實際上並沒有閱讀這個變量(p),所以你沒有收到任何輸入。

沒有在代碼中使用變量p。

要刪除錯誤 - 有必要刪除變量「p」。

1

它並不是一個真正的錯誤: