【问题标题】:Count Chars in a String Java计算字符串 Java 中的字符数
【发布时间】:2012-02-13 11:40:52
【问题描述】:

您好,我的代码有问题,我必须计算表达式中使用的变量,当两个或多个变量相同时,应计为 1。例如,a+ab = 使用的变量总数: 2. 问题是当我输入 a+a =使用的变量总数:2。 这是我的代码:

public void simplify(String strexp){
    int ex =strexp.length();

    for (int a=0;a<=ex-1;a++){
        if(a==0)
        {
        if (Character.isLetter(strexp.charAt(a)))
        {
        b++;
       }


        }
        else{
        for(int c=0;c<=a-1;c++){
              if (Character.isLetter(strexp.charAt(c))==Character.isLetter(strexp.charAt(a)))
              {

                System.out.println("equal");
                break;
              }
              else if (Character.isLetter(strexp.charAt(c))!=Character.isLetter(strexp.charAt(a)))
              {
             //if(c==a-1)
              //{
               b++;
               System.out.println("nomatch");
           //  }
               }
        }
        }
        }
   JOptionPane.showMessageDialog(null, b);
    }

【问题讨论】:

  • 您可以使用正则表达式来查找所有变量,然后通过一些数组唯一函数或其他方法。
  • abAB 是否相等?
  • 你要数哪一个:变量数还是字符数?我可以用一个或多个字符命名一个变量;例如:a、变量、ab、a1
  • 因为您的示例包括a + ab,它的变量总数为二(2)。所以,我假设每个变量的长度可以超过一个字符。
  • 对不起,你的标题误导了;它与您想要实现的目标不同......

标签: java string for-loop count


【解决方案1】:

使用列表统计变量总数

public static int simplify(String strexp) {
    int length = strexp.length();

    List<Character> usedVariables = new ArrayList<Character>();
    for (int i = 0; i < length; i++) {
        char c = strexp.charAt(i);
        if (Character.isLetter(c) && !usedVariables.contains(c)) {
            usedVariables.add(c);
        }
    }

    return usedVariables.size();
}

public static void main(String[] args) {
    System.out.println(simplify("x + b = d"));
    System.out.println(simplify("x + x = a"));
    System.out.println(simplify("x + x = x / 2"));
}

打印出来

3
2
1

【讨论】:

  • 非常感谢您的帮助,列表对您有很大帮助
  • @AlvinPulido,很高兴,如果您喜欢答案,也可以投票。 :)
【解决方案2】:

将字符串转换为 char 数组,将它们添加到映射中,其中键是 char,值是其变量计数。

import java.util.*;

    class Main

{
    public static void main (String[] args) 
    {
        String yourString = "UNDERSTAND";

        Map<Character, Integer> count = new TreeMap<Character, Integer>();
        for (char c : yourString.toCharArray()) {
            if (count.containsKey(c)) {
                count.put(c, (int) count.get(c) + 1);
            } else {
                count.put(c, 1);
            }
        }

        System.out.println(count);

    }
}

【讨论】:

  • Map 对于他的情况是不必要的,不需要计数值来计算有多少唯一字符。
【解决方案3】:

您需要跟踪您看到的变量。一种方法可能是使用Set&lt;char&gt;。每当您看到一个角色时,将其添加到集合中。唯一运算符的数量将是集合中的字符数。

【讨论】:

    【解决方案4】:

    这么低的水平很难跟上。为什么不使用正则表达式来查找所有匹配项,然后构建一组唯一的匹配项?

    标题具有误导性。该任务更像是词汇记号计数。比单纯的字符高一级。

    【讨论】:

      【解决方案5】:

      这个单线怎么样?

      String complexExpression = "a + a*2 (2*a + b - 34)/2 + 3*c";
      
      System.out.println(Arrays.toString(complexExpression.split("[+*()-/\\d\\s&&[^a-z]]+")));
      
      System.out.println(new HashSet<String>(Arrays.asList(complexExpression.split("[+*()-/\\d\\s&&[^a-z]]+"))).size());
      

      上面的代码为我打印了以下内容:

      [a, a, a, b, c]
      3
      

      当然,我不建议使用相同的模式多次执行split 操作。

      【讨论】:

      • 您的回答是正确的,因为它考虑了具有一个或多个字符大小的变量:例如,表达式 "ab + ab*2 (2*ab + ba - 34)/2 + 3*c" 给出了一个 [ab, ab, ab, ba, c] 数组,唯一变量计数为 3。跨度>
      【解决方案6】:

      这里有一些东西:

      public class DistinctCharCount {
          public static void main(String[] args) {
              String s = "a+ab+abc+abcd";
      
              HashSet<Character> charSet = new HashSet<Character>();        
              for(int i=0;i<s.length();i++) {
                  charSet.add(s.charAt(i));
              }
      
              System.out.println("Distinct Char Count : " + charSet.size());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-07
        • 2018-04-20
        • 2011-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-15
        • 1970-01-01
        相关资源
        最近更新 更多