質問

here are my requirements:

Create (hard coded) the 50 states and their capital cities, using a 2 dimension array.

In the dialog box: ask the user either to enter the State or a City.

If the state is entered, find its capital city. If a city is entered, find its State.

If not, found, issue an error message.

This should be in a Loop, until the user does not want to play anymore.

I really don't know where to start, all I have done so far is create the array, I don't really get how to search the array, and spit out the corresponding state/capital.

Any help would be greatly appreciated.

Here is the code I have written so far.

import java.util.Scanner;

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

          java.util.Scanner input = new java.util.Scanner(System.in);

     String[][] statesAndCapitols = {
        {"Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"},
        {"Montgomery","Juneau","Phoenix","Little Rock","Sacramento","Denver","Hartford","Dover","Tallahassee","Atlanta","Honolulu","Boise","Springfield","Indianapolis","Des Moines","Topeka","Frankfort","Baton Rouge","Augusta","Annapolis","Boston","Lansing","St. Paul","Jackson","Jefferson City","Helena","Lincoln","Carson City","Concord","Trenton","Santa Fe","Albany","Raleigh","Bismarck","Columbus","Oklahoma City","Salem","Harrisburg","Providence","Columbia","Pierre","Nashville","Austin","Salt Lake City","Montpelier","Richmond","Olympia","Charleston","Madison","Cheyenne"}};

            System.out.println("Please enter a State or a capitol city.");
                String userInput = input.nextLine();



        }
    }

thanks again!

役に立ちましたか?

解決

try searching through the array with a for loop.

Using a for loop it keeps track and updates your current position of traversing the array.

Once you find the correct state or capital (by checking if userInput.equalsIgnoreCase(statesAndCapitols[x][y]), then take the current position you are at and retrieve the information needed.

I.E.

for(int x = 0; x < 2; ++x) //loop through states the first time, capitols the second
  for(int y = 0; y < 50; ++y) //always 50, unless new states get added (obviously not a problem in this example, but useful to think about in future problems - YOUR DATA WILL ALMOST ALWAYS CHANGE.
   if(userInput.equalsIgnoreCase(statesAndCapitols[x][y])
      System.out.println(statesAndCapitols[x == 1 ? 0 : 1][y]);

In the array, I did x == 1 ? 0 : 1. That's a ternary operator, what it's saying is if x is equal to 1, use the value 0, otherwise use the value 1.

That's one way to go about this problem.

Another way would be to create your own Class/Datatype for the cities and states, that way you don't need to keep your arrays in sync, meaning you don't need to update 2 items for one change (like add another city/state combo).

Hope this helps a bit! :)

他のヒント

String entered_state=input.nextLine();
for(int i=0;i<50;i++){
    if(statesAndCapitols[0][i].equals(entered_state)){
        String searched_city=statesAndCapitols[1][i];
        //print the city name
        break;
        }
}
if(i==50) 
//print error

Same thing for searching state from entered city.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top