以下确保 char(4) 的输出,因此 5 是 0005,43 是 0043:
if ( var > 1 && var <= 9 ) {
o.setOutput (String.format ("%0001d", i.getInput());
else if (var > 9 && var <= 99 ) {
o.setOutput (String.format ("%002d", i.getInput());
else if (var > 99 && var <= 999 ) {
o.setOutput (String.format ("%03d", i.getInput());
else if (var > 999 & var <=9999 ) {
o.setOutput (String.format ("%4d", i.getInput());
}
有什么方法可以只写一行而不是多个 if/case 语句吗?
谢谢。
最佳答案
看起来你只是想要:
o.setOutput (String.format ("%04d", i.getInput());
格式字符串 %04d
生成一个(至少)四个字符的输出字段,根据需要在左侧填充 0
个字符。
关于java - 将java语法折叠成单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11665992/