【问题标题】:Having difficulty with logic in basic Java RLE program基本 Java RLE 程序中的逻辑有困难
【发布时间】:2021-09-16 14:06:39
【问题描述】:

目前正在学习 Java,对这个相当基本的 RLE 程序有困难。 我认为错误与重置字符数有关吗?它似乎正在输出应该被移出的以前的值。

请帮忙!我觉得自己太笨了,我不知道如何解决它哈哈

例如格式化:AABCEDDDGHIIIII 将被转换为 A2B1C1E1D3G1H1I5

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    
    System.out.println("Enter string for encoding: ");
    String uString = input.next();

    int count = 1;
    String eString = "";

    for (int i = 0; i < uString.length(); i++){
      if ((i != 0) && (uString.charAt(i) == uString.charAt(i-1))){
        count++;
      }else if(i!= 0){
        eString = eString + uString.charAt(i) + String.valueOf(count);
        count = 1;
      }
      System.out.println(String.valueOf(uString.charAt(i)) + String.valueOf(i) + String.valueOf(count)); //just to see what's happening in my code
    }System.out.println("Unencoded string's length: " + uString.length() + "\nEncoded string:" + eString + ", encoded string's length: " + eString.length());
  }
}```

【问题讨论】:

  • edit您的问题提供您当前的输出和预期的输出。这将有助于人们帮助您,而无需自己提取代码并运行它。
  • 你已经接近了,所以我不会发布答案,让你能够先处理它,你不需要使用 String.valueOf(count);只需使用 count 当 i=0 它不会做任何事情时它会被附加到你的 for 循环中的字符串然后当它为 1 时它会找到两次所以计数为 2 然后当它执行第二个 if 语句时if part so it make the current char B be 2 not which it should make the old char which is on i-1 to be 2 another issue that last case i will not be counted 使用您的 ide 调试模式,它将帮助您识别问题
  • 忘记提及,当您像使用 eString 变量一样对字符串进行大量修改时,使用 docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/… stringbuilder 比使用 string 更好

标签: java logic logic-error


【解决方案1】:

逻辑中的主要错误是此代码缺少 当前 字符,每个count 递增。

因此,在eString = eString + uString.charAt(i) + String.valueOf(count); 行中,下一个 字符被添加到结果字符串中。

可以通过在循环前添加第一个字符、从索引 1 开始循环并在循环后添加计数器来修复逻辑。

条件if (i != 0) 可能会被完全删除:

String eString = "";

if (!uString.isEmpty()) {// there's something to count

    int count = 1;
    String eString = "" + uString.charAt(0);

    for (int i = 1, n = uString.length(); i < n; i++) {
        if (uString.charAt(i) == uString.charAt(i-1)) {
            count++;
        } else {
            // append count for previous character and then the current char
            eString = eString + count + uString.charAt(i);
            count = 1;
        }
        System.out.println(String.valueOf(uString.charAt(i)) + i + count); //just to see what's happening in my code
    }
    eString = eString + String.valueOf(count);
}
System.out.println("Unencoded string's length: " + uString.length() + "\nEncoded string:" + eString + ", encoded string's length: " + eString.length());

另一种方法是使用 empty 嵌套循环(和 StringBuilder 来更有效地连接字符和计数器):

StringBuilder sb = new StringBuilder();
for (int i = 0, n = uString.length(); i < n; i++) {
    int count = 1;
    char c = uString.charAt(i);
    // loop without body just to increment counter
    for (int j = i + 1; j < n && uString.charAt(j) == c; j++, i++, count++);

    sb.append(c).append(count);
}
String eString = sb.toString();

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多