2016-11-28 85 views
-1

對於學校我必須製作一個掃描器,它能夠接受字符串輸入並計算1「a」等元音的數量。我的問題是,當我輸入一個字符串時,它不會經過循環,我可以連續輸入和輸入什麼都不做。 (我使用BlueJ來編碼)。謝謝你的幫助!Java代碼沒有處理輸入

package VowelCounter; 

/** 
* Description: 
* Author: Jack Cannon 
* Date: 11/15/16 
*/ 

import java.util.Scanner; 
public class VowelCounter 
{ 
public static void main(String [] args) 
{ 
    Scanner input = new Scanner(System.in); 
    //Prompt the user to enter a phrase 
    System.out.println("Type in a sentence."); 
    String phrase = input.nextLine(); 
    //as long as the string isn't "quit", 
    //count the vowels in the string. 
    int length = phrase.length(); 
    while(phrase != "quit") 
    { 
     int a = 0; 
     int e = 0; 
     int i = 0; 
     int o = 0; 
     int u = 0; 
     for(int c = 0; length < 1; length++) 
     { 
      char vowels = phrase.charAt(c); 
     if(vowels == 'a') 
     { 
     } 
     else if(vowels == 'e') 
     { 
     } 
     else if(vowels == 'i') 
     { 
     } 
     else if(vowels == 'o') 
     { 
     } 
     else if(vowels == 'u') 
     { 
     } 
     } 
    }  
    System.out.println("There are " + 'a' + "a's"); 
    System.out.println("There are " + 'e' + "e's"); 
    System.out.println("There are " + 'i' + "i's"); 
    System.out.println("There are " + 'o' + "o's"); 
    System.out.println("There are " + 'u' + "u's"); 
    //print out the count of the vowels for this string.  
    //prompt the user for another phrase. 
    } 
    } 
+1

你永遠只要求輸入一次......也使用字符串的equals方法Java中 –

+0

比較字符串時是否有您使用的環路長度<1的原因嗎?這不會遍歷整個字符數。也可以看看使用開關盒 –

回答

1

上面分享的代碼有幾個錯誤。

首先,你的while循環的條件是phrase != "quit"。這有兩個原因是錯誤的。首先,這不是你如何比較java中的字符串。爲此,請參閱this。第二個原因是你並沒有改變你的while循環中的變量phrase,所以這將導致無限循環,因爲phrase將永遠不會「退出」,除非這是用戶輸入的內容。如果你只需要輸入用戶一次,我沒有看到這個while循環的需要。

你的代碼錯誤的第二個原因是你的for循環中的if語句(查看當前char是什麼字符)什麼都不做。在這一點上,它們只是空白的代碼塊。很可能你需要做的是增加適當的元音計數器。

最後,你在你的打印線使用不正確的字符串連接方法。在Java中進行協調的正確方法如下:

"There are " + a + "a's" // a = 1 
=> "There are 1 a's" 

使用單引號是不正確的;你可以使用,如果你想打印的字母「A」,像這樣:

"There are a a's" 
0

這應該可以解決你的問題,我只加2,如果在循環語句,你可以添加休息。

public static void main(String [] args){ 
    Scanner input = new Scanner(System.in); 
    //Prompt the user to enter a phrase 
    System.out.println("Type in a sentence."); 
    String phrase = input.nextLine(); 
    //as long as the string isn't "quit", 
    //count the vowels in the string. 
    int length = phrase.length(); 
    int a = 0; 
    int e = 0; 
    int i = 0; 
    int o = 0; 
    int u = 0; 
    while(!phrase.equals("quit")){ 
     for(int c = 0; c<length; c++){ 
      char vowels = phrase.charAt(c); 
      if(vowels == 'e'){ 
       e++; 
      } 
      else if(vowels == "o"){ 
       o++; 
      } 
     } 
     System.out.println("There are " + a + "a's"); 
     System.out.println("There are " + e + "e's"); 
     System.out.println("There are " + i + "i's"); 
     System.out.println("There are " + o + "o's"); 
     System.out.println("There are " + u + "u's"); 
     System.out.println("enter another sentence"); 
     phrase = input.nextLine(); 
    }  
}