2016-04-21 37 views
0
public String toString() 
{ 
if(zipCode < 10000) 
{ 
    if(zipCode < 1000) 
    { 
     if(zipCode < 100) 
     { 
      if(zipCode < 10) 
      { 
       return "0" + "0" + "0" + "0" + Integer.toString(zipCode); 
      } 
      return "0" + "0" + "0" + Integer.toString(zipCode); 
     } 
     return "0" + "0" + Integer.toString(zipCode); 
    } 
    return "0" + Integer.toString(zipCode); 
} 
else 
{ 
    return Integer.toString(zipCode); 
} 
} 

有没有一个循环,我可以做到这一点,将在字符串前面添加一个零,这取决于存储在zipCode中的数字的指数是多少?我不想摆脱我想保持零的零点,但零点被拿走。我怎么能把它变成一个循环?

+0

不,我期望保持零位 – Ethan

回答

2
String.format("%05d", number); 

它已经实施。

+0

谢谢,我的工作方式到底需要多少 – Ethan

相关问题