我一直在浏览网站,无法找到问题的答案。我需要能够查看给定数量的字符串并计算单词中的元音量。如果元音量与之前输入的数字相匹配,则打印YES。如果一个单词没有正确数量的元音,请打印NO。我尝试使用一个String数组然后遍历数组中的每个单词来查找元音,为它找到的每个元音增加元音检查(vocheck),然后检查vocheck是否等于适当的数字。但是,当我检查vocheck for loop是否有效时,它只是将vocheck保持为0.什么错了?
import java.util.*;
public class verses {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int lines = input.nextInt();
int[] arr = new int[lines];
int vowels = 0;
boolean vcheck = true;
int vocheck = 0;
for(int i = 0; i < lines; i++){
arr[i] = input.nextInt();
}
String[] word = new String[lines];
for(int x = 0; x < word.length; x++){
word[x] = input.nextLine();
}
word[word.length-1] = input.nextLine();
for(int m = 0; m < lines; m++){
for(int y = 0; y < word[y].length(); y++){
if(word[m].charAt(y) == 'a'
|| word[m].equals('e')
|| word[m].equals('i')
|| word[m].equals('o')
|| word[m].equals('u')
|| word[m].equals('y')){
vocheck++;
}
}
if(vocheck != arr[m]){
vcheck = false;
}
}
if(vcheck == true){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
这是示例输入的样子 3 2 2 3 英特尔 码 挑战
这应该打印YES,因为有三个单词,第一个有2个元音,第二个有2个元音,第三个有3个元音
答案 0 :(得分:0)
删除这些单词[m] .equals(&#39; e&#39;)并像第一次使用一样使用 字[m] .charAt(y)==&#39; a&#39;。
和 for(int y = 0; y&lt; word [y] .length(); y ++)//使用word [m]
public class Test {
public static void main(String[] args) {
int[] arr = {2, 3, 2};
boolean vcheck = true;
String[] word = {"ch ale", "fa jio", "gt eyu"};//new String[lines];
int vocheck = 0;
for(int m = 0; m < 3; m++){
vocheck = 0;
for(int y = 0; y < word[m].length(); y++){
if(word[m].charAt(y) == 'a'
|| word[m].charAt(y) == 'e'
|| word[m].charAt(y) == 'i'
|| word[m].charAt(y) == 'o'
|| word[m].charAt(y) == 'u')
{
vocheck++;
}
}
System.out.println(vocheck);
if(vocheck != arr[m]){
vcheck = false;
break;
}
}
if(vcheck == true){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
试试吧
答案 1 :(得分:0)
您可以这样做:
for(int i = 0; i < lines; i++){
if(word[i].toLowerCase().replaceAll("[^aeiouy]", "").length() == arr[i]){
System.out.println("Word " + word[i] + " has " + arr[i] + " vowels");
}else{
System.out.println("Word " + word[i] + " DOESN'T HAVE " + arr[i] + " vowels");
}
}
这个word[i].toLowerCase().replaceAll("[^aeiouy]", "").length()
做的是将单词中的所有字母更改为小写(toLowerCase()
),替换其中的所有非元音字符(replaceAll("[^aeiouy]", "")
)并获取剩余的数字字符(length()
),元音数量。
答案 2 :(得分:0)
这里有很多问题:
input.nextLine();
。这是必要的,否则数组word
中的第一个条目将是前一行的其余部分。word[word.length-1] = input.nextLine();
没有必要vocheck
for(int y = 0; y < word[y].length(); y++){
应为for(int y = 0; y < word[m].length(); y++){
。注意y
已更改为m
。其他条件也不一致。它们不应该像word[m].equals('e')
,而应该像word[m].charAt(y) == 'e'
import java.util.*;public class verses {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int lines = input.nextInt();
int[] arr = new int[lines];
int vowels = 0;
boolean vcheck = true;
int vocheck = 0;
for(int i = 0; i < lines; i++){
arr[i] = input.nextInt();
}
input.nextLine();
String[] word = new String[lines];
for(int x = 0; x < word.length; x++){
word[x] = input.nextLine();
}
//word[word.length-1] = input.nextLine();
for(int m = 0; m < lines; m++){
vocheck = 0;
for(int y = 0; y < word[m].length(); y++){
if(word[m].charAt(y) == 'a'
|| word[m].charAt(y) == 'e'
|| word[m].charAt(y) == 'i'
|| word[m].charAt(y) == 'o'
|| word[m].charAt(y) == 'u'
|| word[m].charAt(y) == 'y'){
vocheck++;
}
}
if(vocheck != arr[m]){
vcheck = false;
}
}
if(vcheck == true){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
答案 3 :(得分:0)
你为什么不尝试一些正则表达式:)
public static void main(String[] args) throws InterruptedException {
String s = "4 2 2 3 2 intel code challenge aa";
s = s.replaceAll("(?i)[^\\daeiou ]", ""); // replace all consonants with empty String.
String[] arr = s.split("\\s+"); // Split based on space.
int count = 0;
System.out.println(Arrays.toString(arr));
for (int i = 1; i <= arr.length / 2; i++) {
// Check for equality of String lengths and input values
if (Integer.parseInt(arr[i]) == arr[arr.length / 2 + i].length()) {
count++;
} else {
System.out.println("NO");
return;
}
}
// Check if counts of String are same as number of Strings
if (count != Integer.parseInt(arr[0])) {
System.out.println("NO");
} else {
System.out.println("YES");
}
}
O / P:
[4, 2, 2, 3, 2, ie, oe, aee, aa]
YES