如何在print语句中使用零填充标志正确打印​​两个位置

dad*_*ona 7 java printf

如果想用零垫打印这个方法怎么做呢

int month, day;

public void  printNumeric()
{
  System.out.printf("month +"/" +day +" \n");
  // i would like the month if it is 5 to be 05 same thing with the day
}
Run Code Online (Sandbox Code Playgroud)

ZZ *_*der 10

int month, day;

public void  printNumeric()
{
  System.out.printf("%02d/%02d\n", month, day);
  // i would like the month if it is 5 to be 05 same thing with the day
}
Run Code Online (Sandbox Code Playgroud)


mik*_*iku 5

您可以使用java.util.FormatterString.format

String.format("%02d", somenumber)
Run Code Online (Sandbox Code Playgroud)