2014-03-25 33 views
0

我完全失去了從這裏做什麼。如何讓要處理的字符串不被輸入爲命令行參數 ,而是作爲從標準輸入中讀取的一行文本,並提示輸入問號?計數元音 - 掃描儀,字符串

當前代碼

import java.util。*;

公共類CountVowels {

public static void main (String[] args) { 
     if (args.length ==1) { 
     String str = args [0]; 
     int ofVowels = 0; 
    for(int i=0;i <str.length();i++){ 
     if((str.charAt(i) == 'a') || 
      (str.charAt(i) == 'e') || 
      (str.charAt(i) == 'i') || 
      (str.charAt(i) == 'o') || 
      (str.charAt(i) == 'u')) { 
      ofVowels ++; 


     } 

    } 
    System.out.println(" In The String "+str+", there are "+ofVowels+" vowels"); 
} 
} 
} 

回答

1

首先,你需要有import java.util.Scanner;

那你去:

Scanner s = new Scanner(System.in); // create and attach the scanner 
System.out.println("?"); 
String str = s.nextLine(); // gets what the user typed 

編輯:添加到您的代碼,這使得它:

Scanner s = new Scanner(System.in); // create and attach the scanner 
System.out.println("?"); 
String str = s.nextLine(); // gets what the user typed 
int ofVowels = 0; 
for(int i=0;i <str.length();i++){ 
    if((str.charAt(i) == 'a') || 
     (str.charAt(i) == 'e') || 
     (str.charAt(i) == 'i') || 
     (str.charAt(i) == 'o') || 
     (str.charAt(i) == 'u')) { 
     ofVowels ++; 
    } 
} 
System.out.println(" In The String "+str+", there are "+ofVowels+" vowels"); 
+1

不返回有多少元音。 – TheDavidTurner

+0

@TheDavidTurner我編輯了答案。我很確定你的代碼的其餘部分是正確的,所以我只是將它修補到它。 –