2013-09-28 19 views
-2

我正在尋找一種方法將值index.totalNumCitations設置爲在while循環中設置的值。我最初將值設置爲1,以便for循環至少執行一次。我試圖獲得for循環之前的值,這也不起作用。如果有人能指引我正確的方向,我會非常感激。設置具有參數的值

for (int i = 0; i < index.totalNumCitations; i++) { 


     while (inputInteger > 50 || inputInteger < 0) { 
      inputInteger = Integer.parseInt(sc.nextLine()); 
      index.Index(inputInteger); 
    } 

這是在while循環

public void Index(int totalNumCit) { 
    if (totalNumCit <= 50 && totalNumCit > 0) { 
     this.totalNumCitations = totalNumCit; 
     citationIndex = new Citation[totalNumCit]; 
    } else { 
     System.out.println("Error: Please enter a number between 0 and 50."); 
    } 
} 
+0

什麼類型是'index'變量? –

+0

你在問什麼? –

+0

這是一堂課。我不確定這是否是你所指的。 – user2800960

回答

1

使用時問一個問題的方法,嘗試更一般地描述你想要做什麼。 人們可能會想出更好的解決方案。

但是對於這種情況,我建議你在for循環之前放置while循環。因爲如果不在for循環中的其他地方更改inputInteger的值,則while循環只會在for循環的第一個循環中運行。

while (inputInteger > 50 || inputInteger < 0) { 
      inputInteger = Integer.parseInt(sc.nextLine()); 
      index.Index(inputInteger); 
} 

for (int i = 0; i < index.totalNumCitations; i++) { 

    //whatever you want to do 

}