第五章第四十九题(对元音和辅音进行计数)(Count vowels and consonants)
生活随笔
收集整理的這篇文章主要介紹了
第五章第四十九题(对元音和辅音进行计数)(Count vowels and consonants)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
*5.49(對元音和輔音進行計數)假設字母A、E、I、O、U為元音。編寫一個程序,提示用戶輸入一個字符串,然后顯示字符串中元音和輔音的數目。
Enter a string: Programming is fun
The number of vowels is 5
The number of consonants is 11
*5.49 (Count vowels and consonants)(Count vowels and consonants) Assume that the letters A, E, I, O, and U are vowels. Write a program that prompts the user to enter a string, and displays the number of vowels and consonants in the string.
Enter a string: Programming is fun
The number of vowels is 5
The number of consonants is 11
下面是參考答案代碼:
import java.util.*;public class CountVowelsAndConsonantsQuestion49 {public static void main(String[] args) {String userString;int vowelCount,consonantCount;vowelCount = consonantCount = 0;System.out.print("Enter a string: ");Scanner inputScanner = new Scanner(System.in);userString = inputScanner.nextLine();for(int strIndex = 0;strIndex < userString.length(); strIndex++){if("aeouiAEOUI".contains(""+userString.charAt(strIndex)))vowelCount++;else if(Character.isLetter(userString.charAt(strIndex)))consonantCount++;}System.out.printf("The number of vowels is %d\n", vowelCount);System.out.printf("The number of consonants is %d", consonantCount);inputScanner.close();} }運行效果:
注:編寫程序要養成良好習慣
1.文件名要用英文,具體一點
2.注釋要英文
3.變量命名要具體,不要抽象(如:a,b,c等等),形式要駝峰化
4.整體書寫風格要統一(不要這里是駝峰,那里是下劃線,這里的邏輯段落空三行,那里相同的邏輯段落空5行等等)
5.普通變量,方法名要小駝峰,類名要大駝峰,常量要使用全部大寫加上下劃線命名法
6.要學習相應的代碼編輯器的一些常用快捷鍵,如:快速對齊等等
總結
以上是生活随笔為你收集整理的第五章第四十九题(对元音和辅音进行计数)(Count vowels and consonants)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 三主_MySQL主主复制3
- 下一篇: 乌班图系统的MySQL_乌班图系统mys