我有以下程序,用户在其中输入他的社会安全号码并分配一个与之对应的位置。
我没有得到的一件事是如何添加一个循环来搜索我的数组,直到找到正确的区域编号并且它对应的区域例如 3 对应于新罕布什尔州。我在我的程序中尝试了一个循环,但我不确定如何让它工作。
import jpb.*;
public class SSNInfo
{
public static void main(String[] args)
{
SimpleIO.prompt("Enter a Social Security number: ");
String ssn = SimpleIO.readLine().trim();
while (true)
{
if (ssn.length() != 11)
{
System.out.println("Error: Number must have 11 " +
"characters");
}
else if (ssn.charAt(3) != '-' ||
ssn.charAt(6) != '-')
{
System.out.println(
"Error: Number must have the form ddd-dd-dddd");
}
else
break;
SimpleIO.prompt("\nPlease re-enter number: ");
ssn = SimpleIO.readLine().trim();
}
// Get the area number (the first 3 digits of the SSN)
int area = Integer.parseInt(ssn.substring(0, 3));
int[] areaNumbers =
{ 3, 7, 9, 34, 39, 49, 134, 158, 211, 220,
222, 231, 236, 246, 251, 260, 267, 302, 317, 361,
386, 399, 407, 415, 424, 428, 432, 439, 448, 467,
477, 485, 500, 502, 504, 508, 515, 517, 519, 520,
524, 525, 527, 529, 530, 539, 544, 573, 574, 576,
579, 580, 584, 585, 586, 588, 595, 599, 601, 626,
645, 647, 649, 653, 658, 665, 675, 679, 680};
String[] locations =
{"New Hampshire", "Maine", "Vermont",
"Massachusetts", "Rhode Island", "Connecticut",
"New York", "New Jersey", "Pennsylvania",
"Maryland", "Delaware", "Virginia",
"West Virginia", "North Carolina", "South Carolina",
"Georgia", "Florida", "Ohio",
"Indiana", "Illinois", "Michigan",
"Wisconsin", "Kentucky", "Tennessee",
"Alabama", "Mississippi", "Arkansas",
"Louisiana", "Oklahoma", "Texas",
"Minnesota", "Iowa", "Missouri",
"North Dakota", "South Dakota", "Nebraska",
"Kansas", "Montana", "Idaho",
"Wyoming", "Colorado", "New Mexico",
"Arizona", "Utah", "Nevada",
"Washington", "Oregon", "California",
"Alaska", "Hawaii", "District of Columbia",
"Virgin Islands", "Puerto Rico", "New Mexico",
"Pacific Islands", "Mississippi", "Florida",
"Puerto Rico", "Arizona", "California",
"Texas", "Utah", "New Mexico",
"Colorado", "South Carolina", "Louisiana",
"Georgia", "Arkansas", "Nevada"};
}
int i;
for (i = 0; i < areaNumbers.length; i++)
if areanumbers[i] == int area;
break;
}