我是 Java 初学者。 我的程序以表格形式给出输出,它从名为 **calculateShoes 的方法返回。我只是想知道是否可以在 return 存在时执行 printf 。我尝试放置 System.out.printf (在 return 语句之后)来使用“|%3d|”用于间距,但它不起作用。
@Override
公共(public)字符串 toString()
{
返回 "| "+ this.numberOfShoes + "| "+ this.sumOfShoesInputted
+ "| "+ this.lowestNumberOfShoes + "到 "+ this.highestNumberOfShoes + "|";
}
我意识到这看起来很乱,并且输出的数据移动了“|”。我希望将其修复,而不是一直调整大小。我希望有一种更干净的方法来做到这一点。
**抱歉,我使用了错误的 boolean 方法>_<。
最佳答案
您可以尝试使用String.format()
:
return String.format("|%3d|", some_variable);
此方法的工作方式类似于System.out.printf()
关于java - 是否可以实现tostring字符串方法和printf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22086252/