是否可以默认设置但以编程方式设置shiftnor?我的意思是如果我有代码
System.out.format("%-d%d", shift, value);
它返回java.util.MissingFormatWidthException
。我想动态设置类次,而不是错误。
我怎样才能做到这一点?
最佳答案
如果我理解正确,您可以即时创建格式字符串,但这必须是一个两步过程:
String formatString = "%-" + shift + "d";
System.out.format(formatString, value);
或
String formatString = String.format("%%-%sd", shift); // %% for the single %
System.out.format(formatString, value);
或者作为基督教笔记
System.out.format("%-" + shift + "d", value);
关于java - 如何动态设置 String.format 中的移位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27789312/