ユーザーが自分の社会保障番号を入力し、それに対応する場所が割り当てられる次のプログラムがあります。
私が得られないことの 1 つは、正しいエリア番号とそれに対応するエリア (たとえば 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;
}