简体   繁体   English

删除最后一个新行字符串

[英]Deleting last new line string

So I've tried pretty much everything to get rid of the last newline character in my code. 所以我已经尝试了很多东西来摆脱代码中的最后一个换行符。 Its supposed to print a new line after every recursive call except for the last one. 它应该在每次递归调用之后打印一个新行,除了最后一个。 Any ideas? 有任何想法吗?

public static boolean solve(double target, ArrayList<Double> numbers)
{
    String print = "";
    String newPrint = "";
    double compare = 0;

    boolean done = false;

    for (double num : numbers)
    {
        if (!done)
        {

            ArrayList<Double> remaining = new ArrayList<Double>(numbers);

            remaining.remove(num);

            if (target == num)
            {
                done = true;

            }
            else
            {

                done = solve(target + num, remaining);
                if (done)
                {
                    print += ((int) target + (int) num) + " " + "-" + " " + (int) num + " "
                            + "="
                            + " "
                            + ((int) target + "\n");
                }
                else
                {

                    done = solve(target - num, remaining);
                    if (done)
                    {
                        print += ((int) target - (int) num) + " " + "+" + " " + (int) num + " "
                                + "=" + " "
                                + ((int) target + "\n");
                    }
                    else
                    {

                        done = solve(target * num, remaining);
                        if (done)
                        {
                            print += ((int) target * (int) num) + " " + "/" + " " + (int) num
                                    + " " + "=" + " "
                                    + ((int) target + "\n");
                        }
                        else
                        {

                            done = solve(target / num, remaining);
                            if (done)
                            {
                                print += ((int) target / (int) num) + " " + "*" + " "
                                        + (int) num
                                        + " " + "="
                                        + " " + ((int) target + "\n");
                            }
                        }
                    }
                }

            }
        }

    }

    System.out.print(print);

    return done;
  }
}

For instance: 例如:

void recursiveF(...) {
    if ... {
        recursiveF(...);
        println();
    }
    ...
}

结果是一个字符串但你不想要最后一个字符,所以删除它:

print = print.substring(0, print.length()-1);

Use trim() method. 使用trim()方法。 It will remove white spaces, newline etc from beginning and end of string. 它将从字符串的开头和结尾删除空格,换行符等。

System.out.print( print.trim() ); System.out.print( print.trim() );

Short Print the new line character before. 打印之前的新行字符。

The first thing the recursive function should do is print the new line character. 递归函数应该做的第一件事是打印换行符。 This way you shift the odd case from the last function call, to the first call, which should not prepend a new line character. 这样就可以将奇数情况从最后一个函数调用转移到第一个调用,该调用不应该在前面添加一个新行。

You could add a parameter String prepend = "" to the signature of the recursive method, it's empty on the first call, all further calls have it set to String prepend = "\\n" . 你可以在递归方法的签名中添加一个参数String prepend = "" ,在第一次调用时它是空的,所有进一步的调用都将它设置为String prepend = "\\n" Then you can just print prepend without bothering with its content. 然后你可以打印prepend而不用打扰它的内容。

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

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