1

这就是问题所在:给定一个字符串,计算以 'y' 或 'z' 结尾的单词的数量——所以 "heavy" 中的 'y' 和 "fez" 中的 'z' 计数,但不包括 'y ' 在“黄色”中(不区分大小写)。如果单词后面没有紧跟字母,我们会说 ay 或 z 位于单词的末尾。(注意:Character.isLetter(char) 测试 char 是否为字母。)

countYZ("fez day") → 2
countYZ("day fez") → 2
countYZ("day fyyyz") → 2

这是我的代码:

public int countYZ(String str) {
  int count = 0;
  for (int i=0; i<str.length(); i++){
  if (Character.isLetter(i) && (Character.isLetter(i+1)==false || i+1==str.length()) && (Character.toLowerCase(str.charAt(i))=='y' || Character.toLowerCase(str.charAt(i))=='z')){
  count++;
  }
  }
  return count;
}

我知道这很混乱,但我只是想弄清楚为什么它现在不起作用。每次运行都返回“0”。在 if 语句中,我正在检查:是 ia 字母吗?i+1 是字母还是字符串的结尾?最后,如果 i 是 'y' 或 'z'。感谢帮助!

4

10 回答 10

5

您可以使用正则表达式:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public int countYZ(String str) {
    int count = 0;
    Pattern regex = Pattern.compile("[yz](?!\\p{L})", Pattern.CASE_INSENSITIVE);
    Matcher regexMatcher = regex.matcher(str);
    while (regexMatcher.find()) {
        count++;
    } 
    return count;
}

解释:

[yz]      # Match the letter y or z
(?!\p{L}) # Assert that no letter follows after that
于 2013-09-06T05:53:55.937 回答
3

使用split()endsWith()

public static int countYZ(String str) {
        int count = 0;
        String temp[] = str.split(" ");
        for (int i = 0; i < temp.length; i++) {
            if (temp[i].trim().endsWith("y") || temp[i].trim().endsWith("z"))
                count++;
        }
        return count;
    }

输出:根据需要适用于您的所有情况

countYZ("fez day") → 2
countYZ("day fez") → 2
countYZ("day fyyyz") → 2
于 2013-09-06T05:56:28.580 回答
2

试试这个修复

    for (int i = 0; i < str.length(); i++) {
        if ((Character.toLowerCase(str.charAt(i)) == 'y' || Character
                .toLowerCase(str.charAt(i)) == 'z')
                && i == str.length() - 1
                || !Character.isLetter(str.charAt(i + 1))) {
            count++;
        }
    }
于 2013-09-06T05:58:18.743 回答
1

尝试这个

public class CountXY {

    /**
     * @param args
     */
    public static int countXY(String str){
        int count = 0;
        String strSplit[] = str.split(" ");
        for(String i:strSplit){
            if(i.endsWith("y")||i.endsWith("z")||i.endsWith("Y")||i.endsWith("Z")){
                count++;
            }
        }
        return count;   
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "Abcy Asdz z z z y y y yyu ZZ Y ";
        System.out.println("Count::"+countXY(str));
    }

}
于 2013-09-06T06:06:23.207 回答
1
public static int countYZ(String str) {
        String[] array = str.split("[^a-zA-Z]");
        int count = 0;
        for (String s : array) {
            if (s.toLowerCase().trim().endsWith("y") || s.toLowerCase().trim().endsWith("z"))
                count++;
        }
        return count;
    }
于 2018-11-29T10:04:01.840 回答
0

你也可以试试这个

 public static void main(String[] args){
    System.out.println(countYZ("abcxzy"));
}

public static int countYZ(String str) {
    int countYandZ=0;
    String[] arr=str.split(" ");
    for (String i:arr){
        if(("Y".equalsIgnoreCase(String.valueOf(i.charAt(i.length()-1))))||("Z".equalsIgnoreCase(String.valueOf(i.charAt(i.length()-1))))){
              countYandZ++;
        }
    }
    return countYandZ;
}
于 2013-09-06T06:10:46.680 回答
0

您的代码不起作用,因为以下两个条件

Character.isLetter(i) --> 在这里你检查 isLetter 的 i 是 int

(Character.isLetter(i+1)==false -> 会导致 indexout of error

请检查以下我已经检查了它的工作正常,它只是你的代码的修改版本

public class FirstClass {
    public static void main(String args[]) {
        String string="fez day";
          int count = 0;
         String[] strcheck = string.split(" "); 
         for (String str : strcheck) {
          if (Character.isLetter(str.charAt(str.length()-1)) &&(Character.toLowerCase(str.charAt(str.length()-1))=='y' || Character.toLowerCase(str.charAt(str.length()-1))=='z')){
              count++;
          }
         }
          System.out.println(count);
        }
}

希望这会有所帮助,祝你好运

于 2013-09-06T06:10:49.500 回答
0
public  int countYZ(String str) {
    int count=0;

    if ( str.charAt(str.length() - 1) == 'z'||
         str.charAt(str.length() - 1) == 'y'|| 
         str.charAt(str.length() - 1) == 'Z'||
         str.charAt(str.length() - 1) == 'Y' ) {
         count += 1;
    }

    for (int i = 0; i < str.length(); i++) {
        if ( i > 0 ) {
            if ( !( Character.isLetter(str.charAt(i)) ) ) {

                if ( str.charAt(i - 1) == 'y' ||
                     str.charAt(i - 1) == 'z' ||
                     str.charAt(i - 1) == 'Y' ||
                     str.charAt(i - 1) == 'Z' ) {
                    count += 1;
                }
            }
        }
    }

    return count;
}
于 2020-02-29T12:41:38.150 回答
0

这是我所做的:

public int countYZ(String str) {
      //Initialize a return integer
      int ret = 0;
      //If it has at least 2 characters, we check both ends to see how many matching instances we have.
      if (str.length() >= 2)
      {
        if (!Character.isLetter(str.charAt(1)) && (str.charAt(0) == 'y' || str.charAt(0) == 'Y' || str.charAt(0) == 'z' || str.charAt(0) == 'Z')) 
        {
          ret++;
        }
        if (Character.isLetter(str.charAt(str.length() - 2)) && (str.charAt(str.length()-1) == 'y' || str.charAt(str.length()-1) == 'Y' || str.charAt(str.length()-1) == 'z' || str.charAt(str.length()-1) == 'Z')) 
        {
          ret++;
        }
      }
      //If it has more than 3 characters, we check the middle using a for loop.
      if (str.length() >= 3)
      {
        for (int i = 2; i < str.length(); i++)
        {
          char testOne = str.charAt(i-2);
          char testTwo = str.charAt(i-1);
          char testThree = str.charAt(i);
          //if the first char is a letter, second is a "YZyz" char, and the third is not a letter, we increment ret by 1.
          if (Character.isLetter(testOne) && (testTwo == 'y' || testTwo == 'Y' || testTwo == 'z' || testTwo == 'Z') && (!Character.isLetter(testThree)))
          {
            ret++;
          }
        }
      }
      return ret;
    }
于 2016-12-20T03:37:15.387 回答
0

这允许单词被除字母以外的任何东西分隔。空格、数字等

public int countYZ(String str) {
  int count = 0;
  String newStr = str.toLowerCase();
  for (int i =0; i < newStr.length(); i++){
    if (!Character.isLetter(newStr.charAt(i))){
      if (i > 0 && (newStr.charAt(i-1) == 'y' || newStr.charAt(i-1) == 'z'))
        count++;
    }
  }
  if (newStr.charAt(str.length()-1) == 'z' || newStr.charAt(str.length()-1) == 'y')
    count++;
  return count;
}
于 2020-07-20T12:18:44.387 回答