java不断更改第一个邮政编码

MyH*_*rts 0 java

我不明白为什么当我编译这段代码时,我得到了错误的邮政编码.

John Smith
486 test St.
Yahoo, MA 898 - 2597JohnSmith
486 test St.
Yahoo, MA 898  2597
Run Code Online (Sandbox Code Playgroud)

public class test
{
  public static void main(String[] args) {

      String firstName = "John";
      String lastName = "Smith";
      int streetNumber = 486;
      String streetName = "test St.";
      String city = "Yahoo";
      String state = "MA";
      int zip =  01602;
      int zipplus4 = 2597;


     System.out.print(firstName + " " + lastName + "\n" + streetNumber + " " + streetName + "\n" + city + ", " + state + " " + zip + " - " + zipplus4);

     System.out.println(firstName + lastName);
     System.out.println(streetNumber + " " + streetName);  
     System.out.println(city + ", " + state + " " + zip + " - " + zipplus4);  

    }

    }
Run Code Online (Sandbox Code Playgroud)

Mik*_*ron 5

当您指定带前导零的数字时,它将被视为八进制(基数为8,而不是十进制基数-10或十六进制基数为16).

01602 octal == 898 decimal
Run Code Online (Sandbox Code Playgroud)

由于Java没有考虑到Zip代码,为了获得所需的效果,请删除前导零,并在打印时格式化它:

System.out.println(city + ", " + state + " " + new java.text.NumberFormat("00000").format(zip) + " - " + new java.text.NumberFormat("0000").format(zipplus4));  
Run Code Online (Sandbox Code Playgroud)