2016-03-31 43 views
-1

這是我的代碼片段。我已經修復了這個錯誤,但我現在不明白它爲什麼起作用。我不確定是否正確解釋了這一點,如有必要,請提出任何問題。錯誤在於變量的文字更改爲正確的值測試當語句位於我評論的地方時,語句有效。但是,當聲明位於我評論的地方時,聲明不起作用,聲明不起作用。與此特定程序的變量範圍相混淆

 if (endOfSen) { 
       /////////////////The statement below works when it is here.///////////// 
       /////////////////OUTPUT 1 - occurs when the statement is placed here.    
       String puncation = null ; 
       ///////////////////// 
       int orgSize = words.size(); //remember size of stack 
       //only runs if stack is not empty 
       if (!words.empty()) { 
        while (words.size() > 0) {  //until last word 
         String word = words.pop(); 
         ///The statement below does not work when it is here/////// 
         //////OUTPUT 2- occurs when the statement is placed here. 
         //String puncation = null ; 

         //if last word of sentence 
         if (orgSize == words.size() + 1) { 
          word = word.substring(0, 1).toUpperCase() + word.substring(1); 
          puncation = "test";  // just a test value 
          word = word.replace(puncation, ""); 
          //////////////////test to see if works 
          System.out.println("puncation: " + puncation); 
         } 
         //if first word of sentence 
         if (words.size() == 0) { 
          //////////////////test to see if works 
          System.out.println("puncation: " + puncation); 
          word = word.toLowerCase(); 
          word = word + "" + puncation; 
         } 
         newSen.push(word); 
        } 
       } 
       endOfSen = false; 
      } 
     } 

輸出1(第二puncation從原始值的變化)

puncation: test 
puncation: test 

輸出2(第二puncation不會從原始值改變)

puncation: test 
puncation: null 
+0

錯誤是什麼?你改變了什麼? – TinyTheBrontosaurus

+0

請指定輸出1何時發生,何時輸出2發生 –

+0

@TinyTheBrontosaurus我做了一個編輯,不知道它是否有幫助。 – name

回答

1

如果變量在循環內聲明,那麼循環的每次迭代都會看到一個di不同的變量。此外,您甚至可以將它初始化爲null,因此每次迭代都將以空值開始。

當在循環外聲明爲null時,變量將保留循環的先前迭代中的值。

由於orgSize在循環中永不改變,並且words每次迭代縮小一次,因此第一次迭代時只有第一個if語句成立。第二個if聲明在上次迭代中只能是真實的。

所以,如果puncation被初始化爲空的循環中,唯一一次可以在第二if聲明不爲空是,如果words原本尺寸1.

簡單的調試也表明這一點,你的所有。

+0

該死的,現在看起來很明顯。誠然,我應該親手寫下該程序正在做什麼,我會看到發生了什麼事情。那麼,謝謝你的幫助。 – name

1

這不是一個範圍問題,因爲它只是變量在循環中被重置。我在下面做了一個例子,這個例子更加明確地說明被重置的值。

if (endOfSen) { 

     /////////////////The statement below works when it is here.///////////// 
     /////////////////OUTPUT 1 ////////////////// 
     String puncation = null ; 
     ///////////////////// 
     int orgSize = words.size(); //remember size of stack 
     //only runs if stack is not empty 
     if (!words.empty()) { 
      while (words.size() > 0) {  //until last word 
       String word = words.pop(); 
       ///The statement below does not work when it is here/////// 
       /////////////////OUTPUT 2 ////////////////// 

       // Puncation is beign reset here, each iteration of the loop 
       puncation = null ; 

       //if last word of sentence 
       if (orgSize == words.size() + 1) { 
        word = word.substring(0, 1).toUpperCase() + word.substring(1); 
        puncation = "test";  // just a test value 
        word = word.replace(puncation, ""); 
        //////////////////test to see if works 
        System.out.println("puncation: " + puncation); 
       } 
       //if first word of sentence 
       if (words.size() == 0) { 
        //////////////////test to see if works 
        System.out.println("puncation: " + puncation); 
        word = word.toLowerCase(); 
        word = word + "" + puncation; 
       } 
       newSen.push(word); 
      } 
     } 
     endOfSen = false; 
    } 
} 
+0

沒有真正解釋*爲什麼*。 – Andreas

+0

就像我在其他評論中提到的那樣,現在看起來如此明顯。儘管感謝您的回答。 – name