用java编写一个程序,分别统计并输出文本文件中元音字母a,e,i,o,u的个数
2个回答
展开全部
public static void main(String[] args) throws Exception {
File f = new File("C:\\Users\\史文天\\Desktop\\新建文本文档1.txt");
InputStream in = new FileInputStream(f);
InputStreamReader reader = new InputStreamReader(in);
// 这句话不要忽略,它对你以后编写高性能IO流有至关重要的理解,我建议你先了解一下缓冲区的概念
BufferedReader buffered = new BufferedReader(reader);
String text = "";
for (String line = buffered.readLine(); null != line; line = buffered.readLine()) {
text += line;
}
buffered.close();
int aTotal = 0;
int eTotal = 0;
int iTotal = 0;
int oTotal = 0;
int uTotal = 0;
for (int index = 0; index < text.length(); index++) {
if ('a' == text.charAt(index))
aTotal++;
if ('e' == text.charAt(index))
eTotal++;
if ('i' == text.charAt(index))
iTotal++;
if ('o' == text.charAt(index))
oTotal++;
if ('u' == text.charAt(index))
uTotal++;
}
System.out.println("a的个数是:" + aTotal);
System.out.println("e的个数是:" + eTotal);
System.out.println("i的个数是:" + iTotal);
System.out.println("o的个数是:" + oTotal);
System.out.println("u的个数是:" + uTotal);
}
File f = new File("C:\\Users\\史文天\\Desktop\\新建文本文档1.txt");
InputStream in = new FileInputStream(f);
InputStreamReader reader = new InputStreamReader(in);
// 这句话不要忽略,它对你以后编写高性能IO流有至关重要的理解,我建议你先了解一下缓冲区的概念
BufferedReader buffered = new BufferedReader(reader);
String text = "";
for (String line = buffered.readLine(); null != line; line = buffered.readLine()) {
text += line;
}
buffered.close();
int aTotal = 0;
int eTotal = 0;
int iTotal = 0;
int oTotal = 0;
int uTotal = 0;
for (int index = 0; index < text.length(); index++) {
if ('a' == text.charAt(index))
aTotal++;
if ('e' == text.charAt(index))
eTotal++;
if ('i' == text.charAt(index))
iTotal++;
if ('o' == text.charAt(index))
oTotal++;
if ('u' == text.charAt(index))
uTotal++;
}
System.out.println("a的个数是:" + aTotal);
System.out.println("e的个数是:" + eTotal);
System.out.println("i的个数是:" + iTotal);
System.out.println("o的个数是:" + oTotal);
System.out.println("u的个数是:" + uTotal);
}
2013-09-22
展开全部
package temp;
import java.util.Scanner;
public class Temp {
/**
* 得到输入的字符串
*
* @return
*/
public String getString() {
Scanner scn = new Scanner(System.in);
String str = scn.next();
return str;
}
/**
* Main方法
*
* @param args
*/
public static void main(String[] args) {
Temp temp = new Temp();
String str = temp.getString();
int count = temp.getCount(str);
System.out.println("元音字母个数为" + count);
}
/**
* 获得数量
*
* @param str
* @return
*/
public int getCount(String str) {
int count = 0;
char[] exam = { 'a', 'e', 'i', 'o', 'u' };
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j < exam.length; j++) {
if (str.charAt(i) == exam[j]) {
count++;
}
}
}
return count;
}
}
import java.util.Scanner;
public class Temp {
/**
* 得到输入的字符串
*
* @return
*/
public String getString() {
Scanner scn = new Scanner(System.in);
String str = scn.next();
return str;
}
/**
* Main方法
*
* @param args
*/
public static void main(String[] args) {
Temp temp = new Temp();
String str = temp.getString();
int count = temp.getCount(str);
System.out.println("元音字母个数为" + count);
}
/**
* 获得数量
*
* @param str
* @return
*/
public int getCount(String str) {
int count = 0;
char[] exam = { 'a', 'e', 'i', 'o', 'u' };
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j < exam.length; j++) {
if (str.charAt(i) == exam[j]) {
count++;
}
}
}
return count;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询