开发者

Why won't this println command start a new line?

开发者 https://www.devze.com 2022-12-24 18:10 出处:网络
Here\'s the relevant code: public static void printBoarders (Territory x) { int t = 0 ; int n = 0 ; for (int i = 0; i<x.borders.length; i++)

Here's the relevant code:

public static void printBoarders (Territory x) 
{
    int t = 0 ; 
    int n = 0 ; 
    for (int i = 0; i<x.borders.length; i++)
    {
        if (x.borders[i] == -1) 
            t = i ; 
    }
    for (int j = 0; j<x.borders.length; j++) 
    {
        if (x.borders[j] == 1) 
            n++ ;
    }

    Territory.translate (t) ;
    System.out.print (" has " + n + " borders: ") ;
    Territory.translate (x.borders) ;
    System.out.println (" ") ; 
}

When I run this, I get everything on开发者_如何学运维 one line without a line break. Why isn't the System.out.println (" ") ; creating a line break?

Here is an example of what the output winds up being:

Northwest Territory, Alberta, Kamchatka, hidavid-names-macbook-pro:~ davidname$

EDIT: the problem was that this method was never being invoked. A different one which i was replacing was. All is well.


None of the code you're showing is what's outputting "Northwest Territory, Alberta, Kamchatka".

What does .translate() do? It's got to be in there.


i dont see it printing has " + n + " borders: either, so im going to say the code is never executed for some reason


I can see only

System.out.print (" has " + n + " borders: ") ;

Actually I don't understand why you see any other output then

" has 5 borders: "


The code snippit above would start a new line. The problem is that the method printBoarders isn't being invoked.


Only the last print is println, the first is just print so only the space " " is printed to a new line at the end...

EDIT: You mean when you call it multiple times?Y


Just quickly scanning this code, it look like the println() does seem to be reached. Actually the I'm not even seeing how you got the output you did. Is this the complete code. This is a great case for writing a small unit test.


I believe it's starting from the middle of the line where the last System.out.print call left it.

from the docs for PrintWriter, e.g.

public void println(String x)

Prints a String and then terminates the line.

0

精彩评论

暂无评论...
验证码 换一张
取 消