为什么这个 println 命令不开始一个新行?

发布于 2024-08-26 14:09:11 字数 746 浏览 13 评论 0原文

这是相关的代码:

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 (" ") ; 
}

当我运行这个代码时,我将所有内容都放在一行上,没有换行符。为什么 System.out.println (" ") ; 不创建换行符?

以下是输出结果的示例:

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

编辑:问题是该方法从未被调用。我要更换的另一件是。一切都很好。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

神经暖 2024-09-02 14:09:11

您显示的代码都没有输出“西北地区,艾伯塔省,堪察加半岛”。

.translate() 是做什么的?它一定在里面。

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.

行至春深 2024-09-02 14:09:11

我也没有看到它打印 has " + n + " borders: ,所以我想说代码由于某种原因永远不会执行

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

烟雨扶苏 2024-09-02 14:09:11

我只能看到

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

实际上我不明白为什么你会看到任何其他输出

" 有 5 个边框:"

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: "

☆獨立☆ 2024-09-02 14:09:11

上面的代码片段将开始一个新行。问题是 printBoarders 方法没有被调用。

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

丑丑阿 2024-09-02 14:09:11

只有最后一次打印是 println,第一次打印只是打印,因此只有空格“”被打印到末尾的新行...

编辑:您的意思是当您多次调用它时?Y

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

层林尽染 2024-09-02 14:09:11

只要快速扫描这段代码,看起来似乎确实到达了 println() 。实际上,我什至没有看到你是如何得到你所做的输出的。这是完整的代码吗?这是编写小型单元测试的一个很好的例子。

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.

乖乖公主 2024-09-02 14:09:11

我相信它是从最后一个 System.out.print 调用留下的行的中间开始的。

来自 PrintWriter 的文档,例如

public void println(String x)

打印一个字符串然后终止
线。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文