为什么这个 println 命令不开始一个新行?
这是相关的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您显示的代码都没有输出“西北地区,艾伯塔省,堪察加半岛”。
.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.
我也没有看到它打印
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我只能看到
实际上我不明白为什么你会看到任何其他输出
I can see only
Actually I don't understand why you see any other output then
上面的代码片段将开始一个新行。问题是 printBoarders 方法没有被调用。
The code snippit above would start a new line. The problem is that the method printBoarders isn't being invoked.
只有最后一次打印是 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
只要快速扫描这段代码,看起来似乎确实到达了 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.
我相信它是从最后一个 System.out.print 调用留下的行的中间开始的。
来自 PrintWriter 的文档,例如
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.