简体   繁体   中英

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. It will remove white spaces, newline etc from beginning and end of string.

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" . Then you can just print prepend without bothering with its content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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