计算字符串Java中的字符数

计算字符串Java中的字符数,java,string,for-loop,count,Java,String,For Loop,Count,嗨,我的代码中有一个问题,我必须计算表达式中使用的变量,当两个或多个变量相同时,应将其计算为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.isL

嗨,我的代码中有一个问题,我必须计算表达式中使用的变量,当两个或多个变量相同时,应将其计算为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);
    }
public void simplify(字符串strexp){
int ex=strexp.length();

对于(int a=0;a将字符串转换为字符数组,将它们添加到映射中,其中键为字符,值为其变量计数

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);

    }
}
import java.util.*;
班长
{
公共静态void main(字符串[]args)
{
String yourString=“理解”;
映射计数=新树映射();
for(char c:yourString.toCharArray()){
如果(集装箱计数(c)){
count.put(c,(int)count.get(c)+1);
}否则{
计数。放置(c,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"));
}

在这样低的层次上很难理解。为什么不使用正则表达式来查找所有匹配项,然后构建一组唯一的匹配项呢

标题有误导性。任务更像是词汇标记计数。它比简单字符高一步。

这一行如何

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

这里有一点:

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());
    }
}
公共类DistinctCharCount{
公共静态void main(字符串[]args){
字符串s=“a+ab+abc+abcd”;
HashSet charSet=新HashSet();

对于(int i=0;iYou可以使用正则表达式查找所有变量,然后通过某个数组唯一函数或其他任何函数。
ab
ab
是否相等?要计算哪一个:变量数还是字符数?我可以用一个或多个字符命名变量;例如:a,variable,ab,a1因为您的示例包括
a+ab
,它给出了两(2)个变量的总数。因此,我假设每个变量的长度可以超过一个字符。很抱歉,您的标题太误导人了;它与您想要实现的内容不同…对于他的情况,
Map
是不必要的,计算有多少个唯一字符不需要计数值。非常感谢您的帮助,列表非常有用help@AlvinPulido,很高兴,如果你喜欢答案,你也可以投赞成票。:)你的答案是正确的,因为它考虑了大小为一个或多个字符的变量:例如一个表达式
“ab+ab*2(2*ab+ba-34)/2+3*c”
给出了一个
[ab,ab,ba,c]
数组,唯一变量计数为3。
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
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());
    }
}