简体   繁体   English

数信

[英]Counting Letters

I am having some difficulties in finishing this piece of code.我在完成这段代码时遇到了一些困难。 Basically, I have to count the amount of times a letter should appear in a given string.基本上,我必须计算一个字母应该出现在给定字符串中的次数。 For example, ABA should output the following例如,ABA 应输出以下内容

"A appears 1 times"
"B Appears 1 times"
"A Appears 1 times"

However the following code that I wrote does the following (It is part of a method)但是,我编写的以下代码执行以下操作(它是方法的一部分)

public static char[] counter(char[] original, char[] manipulated) {
    int counter =0;
        for (int i=0; i<manipulated.length; i++) {
            for (int j=0; j<original.length; j++) {
                if (original[j] == manipulated[i]) {
                    counter++;
                } else {
                    
                }
            }
            System.out.println(manipulated[i] + " appears " + counter + " times");
            counter = 0;
        }
    
    return manipulated;
}

The output is this:输出是这样的:

"A appears 2 times"
"B appears 1 times"
"A appears 2 times"

Which is not wrong, but that is not how I want it.这没有错,但这不是我想要的。 So could you please assist me on this as soon as possible.所以请你尽快帮我解决这个问题。 I know that I am suppose to reset some variable but I am not sure where to actually reset it.我知道我想重置一些变量,但我不确定在哪里实际重置它。

*Some notes: The variable manipulated is just the string that contains no duplicates so original would be abaa and manipulated would be aba :) *一些注意事项:操纵的变量只是不包含重复项的字符串,因此原始的将是 abaa,操纵的将是 aba :)

If your expected behaviour is just "print out the number of times each character appears in a row" then I am not sure why you need the manipulated variable at all.如果您的预期行为只是“打印出每个字符在一行中出现的次数”,那么我不知道为什么您根本需要manipulated变量。 Your current code just accepts it as an argument and then returns it unchanged.您当前的代码只是将其作为参数接受,然后将其原样返回。

So unless I am misunderstanding the problem it could be a much simpler matter of:所以除非我误解了这个问题,否则它可能是一个更简单的问题:

void showConsecutiveCharacterCounts(char[] input) {
    int consecutiveCount = 0;
    /* iterate through all chars */
    for (int i = 0; i < input.length; i++) {
        /* increment count - will be 1 first time */
        consecutiveCount++;
        /* if we are at the end of a sequence of chars
           I.e. end of input OR next char does not match */
        if (i == input.length - 1 || input[i] != input[i + 1]) {
            /* print previous sequence */
            System.out.println(input[i] + " appears " + consecutiveCount + " times");
            /* and reset count */
            consecutiveCount = 0;
        }
    }
}

If you want to know how many times the same character appear in a row, you could change your code to如果您想知道同一字符在一行中出现多少次,您可以将代码更改为

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

  public static String counter(String s) {
    int counter = 0;
    int j;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); ) {
      j = i;
      char c = s.charAt(i);
      for (; j < s.length(); j++) {
        if (counter == 0) {
          sb.append(c);
        }
        if (c == s.charAt(j)) {
          counter++;
        } else {
          break;
        }
      }
      System.out.println(s.charAt(i) + " appears " + counter + " times");
      counter = 0;
      i = j;
    }
    return sb.toString();
  }

UPDATE changed the code so that you do not need to provide two char[] - now you input any String like "AAACCAAAAAAAB" (aka 'original') and the char[] formerly known as 'manipulated' will be returned by the method. UPDATE更改了代码,这样您就不需要提供两个 char[] - 现在您输入任何字符串,如“AAACCAAAAAAAB”(又名“原始”),以前称为“操纵”的 char[] 将由该方法返回。

Result: A appears 3 times C appears 2 times A appears 7 times B appears 1 times ACAB结果: A appears 3 times C appears 2 times A appears 7 times B appears 1 times ACAB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM