问题
I have a home work. I was asked to make a program that will get zipcode from a user input. Then I'm gonna use the first digit only to determine the location. My instructor told me that the data type to be used is int. can you pls help me? thanks
回答1:
You're comparing characters with integers, so currently you're comparing them with the control characters U+0003 and U+0004 - you want to compare them with characters representing the digits:
if (zipCode.charAt(0) <= '3')
...
if (zipCode.charAt(0) >= '4')
if (zipCode.charAt(0) <= '6')
(You might consider using a switch statement too...)
I've no idea whether this is correct in terms of what zip codes mean, but that's the immediate problem with your code.
来源:https://stackoverflow.com/questions/13657991/java-zipcode-program