2015-08-24 195 views
-2

我想要一個for循環的一段代碼,其中用戶輸入的字數他會寫,然後單詞本身,並在最後它應該檢查具體要求(單詞sstart用K增加計數值並且以K開頭的最後一個詞將被註冊)。然而由於某種原因我不知道,它不會循環,不管輸入它只是打印代碼上的所有內容。For循環運行只有一次

package Others; 
import java.util.*; 
public class StartsWithString 
{ 
    public static void main(String[]args) 
    { 
     Scanner sc = new Scanner(System.in); 
     int wordNumber = 0; 
     String words = ""; 
     int wordCount = 0; 
     String lastWord = ""; 

     System.out.println("How many words are you going to write?: "); 
     wordNumber = sc.nextInt(); 

     System.out.println("Write the desired words: "); 
     for(int i = 0; i < wordNumber; i++); 
     { 
      words = sc.nextLine(); 

      if(words.startsWith("K")) 
      { 
       wordCount++; 
       lastWord = words; 
      } 
     } 

     System.out.println("From a total of " + numriFjaleve + " words typed,); 
     System.out.println(wordCount + " started with the letter K."); 
     System.out.println("The last word typed which began with the letter K was: " + lastWord); 
    } 
} 

回答

11
for(int i = 0; i < wordNumber; i++); 

你必須在你的循環定義的結束分號。所以你的循環實際上不是一個循環。這是一個運行一次的獨立code block。刪除分號。

+0

很好,很快。 – user1274820

+0

@ user1274820謝謝。鑑於問題陳述,這是我的第一個停靠港。 – Kon

+0

哦,我的上帝.............我很尷尬,但同時感到困惑。 Eclipse如何不給予某種關注?我的意思是......很抱歉,我是Java新手。浪費你的時間。對不起,謝謝 – Solocid