public class DecimalFormatDemo{ public static void main(String args[]){ int i=0; System.out.println("输入整数:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try{ i = Integer.parseInt(br.readLine()); }catch(IOException e){ e.printStackTrace(); } DecimalFormat format = new DecimalFormat("00");//看这里 String rst = format.format(i); // System.out.println(rst); }}
.......
if(a.length() == 1){
a = "0" + a;
}
......
String sData;sData= ("00"+ sData).substring(sData.length());
public static void main(String args[]){
int i=0;
System.out.println("输入整数:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
i = Integer.parseInt(br.readLine());
}catch(IOException e){
e.printStackTrace();
}
DecimalFormat format = new DecimalFormat("00");//看这里
String rst = format.format(i); //
System.out.println(rst);
}}
同理分钟和秒都一样
期望以后不用再想同样的问题,我们应该积累知识,收藏和分享珍宝。每次用完就扔掉,明天还得来找。看看 IBM 文章 Java 版 : “构造自己的Java类库”.