gpt4 book ai didi

Java字符串格式化问题

转载 作者:行者123 更新时间:2023-12-01 08:51:54 25 4
gpt4 key购买 nike

while(count <= maxNum){
System.out.println("O-------O");
System.out.printf("|" + "%8s", count + "|");
System.out.println("O-------O");
count++;
startTime = System.currentTimeMillis();
//an empty loop! does nothing except keeps checking the continuation condition
//continues as long as the time that has passed is less than millisToWait
while(System.currentTimeMillis() - startTime < millisToWait);
System.out.println("O-------O");

上面是涉及我想要实现的实际输出的代码部分。该代码在很大程度上按预期工作,除了输出(下面是作为图像附加的输出)不是 100% 正确。每个数字的右侧都有一个额外的“O--------O”。

Here's the output

如果我错了,请纠正我,但我相信这与第一个 println 语句有关,但我仍然不确定它是如何在顶部打印一次,然后在侧面打印一次的。如有任何帮助,我们将不胜感激。

最佳答案

Printf 不会自动添加换行符,您必须通过在字符串中添加换行符来手动添加换行符。出于可移植性的原因,我们使用 %n 而不是\n。格式也可以简化。

将第三行更改为:

System.out.printf("|%8s%n|", count);

关于Java字符串格式化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42333964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com