Java 字符串vov=“”; 字符串con=“”; 字符串inp; int len=input.length(); LinkedHashSet vovels=新LinkedHashSet(); LinkedHashSet辅音=新LinkedHashSet(); 对于(int i=0;i
使用Java 字符串vov=“”; 字符串con=“”; 字符串inp; int len=input.length(); LinkedHashSet vovels=新LinkedHashSet(); LinkedHashSet辅音=新LinkedHashSet(); 对于(int i=0;i,java,Java,使用LinkedHashSet,因为它保留顺序,不允许重复 public static void checkVowels(String s){ System.out.println("Vowel Count: " + (s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length())); //Also eliminating spaces, if any for the consonant count
LinkedHashSet
,因为它保留顺序,不允许重复
public static void checkVowels(String s){
System.out.println("Vowel Count: " + (s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length()));
//Also eliminating spaces, if any for the consonant count
System.out.println("Consonant Count: " + (s.toLowerCase().replaceAll("a|e|i|o| |u", "").length()));
}
//检查元音
公共静态布尔isVovel(字符c){
如果(c='a'| c='e'| c='i'| c='o'| c='u'){
返回true;
}
返回false;
}
公共静态void main(字符串[]args){
字符串输入=“沙石是个好孩子”;
字符间;
字符串vov=“”;
字符串con=“”;
字符串inp;
int len=input.length();
LinkedHashSet vovels=新LinkedHashSet();
LinkedHashSet辅音=新LinkedHashSet();
对于(int i=0;i
此示例是一个类,它将文件中的文本作为字符串读取,将文本存储为字符数组,并将数组中的每个元素转换为字符串,然后查看是否匹配辅音正则表达式或元音正则表达式。它根据正则表达式匹配增加辅音计数或元音计数,最后打印出计数
//Check for vowel
public static boolean isVovel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
return true;
}
return false;
}
public static void main(String[] args) {
String input = "shashi is a good boy";
char inter;
String vov = "";
String con = "";
String inp;
int len = input.length();
LinkedHashSet<String> vovels = new LinkedHashSet<String>();
LinkedHashSet<String> consonents = new LinkedHashSet<String>();
for (int i = 0; i < len; i++) {
inter = input.charAt(i);
inp = Character.toString(inter);
if (isVovel(inter)) {
vov = Character.toString(inter);
vovels.add(vov);
}
else {
con = Character.toString(inter);
consonents.add(con);
}
}
Iterator<String> it = consonents.iterator();
while (it.hasNext()) {
String value = it.next();
if (" ".equals(value)) {
it.remove();
}
}
System.out.println(vovels);
System.out.println(consonents);
}
这个示例是一个类,它将文件中的文本作为字符串读取,将文本存储为字符数组,并将数组中的每个元素转换为字符串,然后查看它是否匹配辅音正则表达式或元音正则表达式。它根据正则表达式匹配增加辅音计数或元音计数,最后打印出计数
//Check for vowel
public static boolean isVovel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
return true;
}
return false;
}
public static void main(String[] args) {
String input = "shashi is a good boy";
char inter;
String vov = "";
String con = "";
String inp;
int len = input.length();
LinkedHashSet<String> vovels = new LinkedHashSet<String>();
LinkedHashSet<String> consonents = new LinkedHashSet<String>();
for (int i = 0; i < len; i++) {
inter = input.charAt(i);
inp = Character.toString(inter);
if (isVovel(inter)) {
vov = Character.toString(inter);
vovels.add(vov);
}
else {
con = Character.toString(inter);
consonents.add(con);
}
}
Iterator<String> it = consonents.iterator();
while (it.hasNext()) {
String value = it.next();
if (" ".equals(value)) {
it.remove();
}
}
System.out.println(vovels);
System.out.println(consonents);
}
我需要使用“loops for”执行此函数,请遵循JavaScript中的示例:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;
public class CountVowlesAndConsonants {
public static void main (String [] args) throws IOException{
CountVowlesAndConsonants countVowlesAndConsonants = new CountVowlesAndConsonants();
countVowlesAndConsonants.countConsonatsAndVowles("/Users/johndoe/file.txt");
}
public void countConsonatsAndVowles(String file) throws IOException {
String text = readFile(file);
Pattern c = Pattern.compile("^(?![aeiouy]+)([a-z]+)$");
Pattern v = Pattern.compile("^[aeiouy]+$");
int vowelCount = 0;
int consonantCount = 0;
char [] textArray = text.toLowerCase().toCharArray();
for( char textArraz : textArray ){
String s = String.valueOf(textArraz);
if(c.matcher(s).matches()) {
consonantCount++;
} else if (v.matcher(s).matches()) {
vowelCount++;
}
}
System.out.println("VowelCount is " + vowelCount + " Constant Count " + consonantCount);
}
public String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
try {
while((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
return stringBuilder.toString();
} finally {
reader.close();
}
}
}
我需要使用“loops for”执行此函数,请遵循JavaScript中的示例:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;
public class CountVowlesAndConsonants {
public static void main (String [] args) throws IOException{
CountVowlesAndConsonants countVowlesAndConsonants = new CountVowlesAndConsonants();
countVowlesAndConsonants.countConsonatsAndVowles("/Users/johndoe/file.txt");
}
public void countConsonatsAndVowles(String file) throws IOException {
String text = readFile(file);
Pattern c = Pattern.compile("^(?![aeiouy]+)([a-z]+)$");
Pattern v = Pattern.compile("^[aeiouy]+$");
int vowelCount = 0;
int consonantCount = 0;
char [] textArray = text.toLowerCase().toCharArray();
for( char textArraz : textArray ){
String s = String.valueOf(textArraz);
if(c.matcher(s).matches()) {
consonantCount++;
} else if (v.matcher(s).matches()) {
vowelCount++;
}
}
System.out.println("VowelCount is " + vowelCount + " Constant Count " + consonantCount);
}
public String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
try {
while((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
return stringBuilder.toString();
} finally {
reader.close();
}
}
}
你到底想给我们看什么?你没有涵盖元音的所有情况,辅音是必不可少的字母字符,而不是元音。当有人输入数字或特殊字符,如“K-9”时会发生什么?你想实际向我们展示什么?你没有涵盖元音的所有情况,辅音是必不可少的字母字符,而不是元音。当有人注入数字或特殊字符(如“K-9”)时,会发生什么情况?对于String.contains和编译正则表达式读取之间的性能差异,对于String.contains和编译正则表达式读取之间的性能差异
public static void checkVowels(String s){
System.out.println("Vowel Count: " + (s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length()));
//Also eliminating spaces, if any for the consonant count
System.out.println("Consonant Count: " + (s.toLowerCase().replaceAll("a|e|i|o| |u", "").length()));
}
//Check for vowel
public static boolean isVovel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
return true;
}
return false;
}
public static void main(String[] args) {
String input = "shashi is a good boy";
char inter;
String vov = "";
String con = "";
String inp;
int len = input.length();
LinkedHashSet<String> vovels = new LinkedHashSet<String>();
LinkedHashSet<String> consonents = new LinkedHashSet<String>();
for (int i = 0; i < len; i++) {
inter = input.charAt(i);
inp = Character.toString(inter);
if (isVovel(inter)) {
vov = Character.toString(inter);
vovels.add(vov);
}
else {
con = Character.toString(inter);
consonents.add(con);
}
}
Iterator<String> it = consonents.iterator();
while (it.hasNext()) {
String value = it.next();
if (" ".equals(value)) {
it.remove();
}
}
System.out.println(vovels);
System.out.println(consonents);
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;
public class CountVowlesAndConsonants {
public static void main (String [] args) throws IOException{
CountVowlesAndConsonants countVowlesAndConsonants = new CountVowlesAndConsonants();
countVowlesAndConsonants.countConsonatsAndVowles("/Users/johndoe/file.txt");
}
public void countConsonatsAndVowles(String file) throws IOException {
String text = readFile(file);
Pattern c = Pattern.compile("^(?![aeiouy]+)([a-z]+)$");
Pattern v = Pattern.compile("^[aeiouy]+$");
int vowelCount = 0;
int consonantCount = 0;
char [] textArray = text.toLowerCase().toCharArray();
for( char textArraz : textArray ){
String s = String.valueOf(textArraz);
if(c.matcher(s).matches()) {
consonantCount++;
} else if (v.matcher(s).matches()) {
vowelCount++;
}
}
System.out.println("VowelCount is " + vowelCount + " Constant Count " + consonantCount);
}
public String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
try {
while((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
return stringBuilder.toString();
} finally {
reader.close();
}
}
}
const defaultListVowels = ['a', 'e', 'i', 'o', 'u'];
function isVowel(value) {
return defaultListVowels.indexOf(value) >= 0;
}
function printLetters(values) {
console.log(values + '\r');
}
function vowelsAndConsonants(s) {
var consonants = [];
var vowels = [];
for (let letter of s) {
if (isVowel(letter)) {
vowels.push(letter);
} else {
consonants.push(letter);
}
}
vowels.forEach(printLetters);
consonants.forEach(printLetters);
}