2

まず、これは宿題の問題です。プログラムを終了しましたが、問題なく動作します。しかし、私には少し問題があります。このプログラムは、最初に txt ファイルをオンラインで読み取り、50 州すべてとその略語を収集することになっています。次に、有効な州の略語を入力するようにユーザーに依頼する必要があります。彼らがそれをしたら、オンラインで別の txt ファイルを読み込んで、2 人の議員の状態を返さなければなりません。

私が抱えている問題は、ユーザーに状態を入力するように求めるスキャナーをループしたいということです。彼らが間違った状態に入った場合、私は彼らに再質問したい. 現在、正当な状態に入ると、プログラムは正常に動作します。間違った状態に入ると、プログラムがループし、別の状態に入るように再度要求されます。間違った状態に入ってから正しい状態に入ると、プログラムを続行するのではなく、別の状態に入るように求められます。何か助けてください。

問題は、プログラムがループするときに読み取れないことだと思います。

if(stateAbbreviation.equals(abbreviation))
{
  state = splitData[0];
  abbrev = stateAbbreviation;
  found = true;
  break;
}

繰り返しますが、found を常に = false にします。

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

public class Senator
{
  static String abbrev; 
  static String state;

  public static void main(String[] args) throws IOException
  {
    // define our URL
    URL serviceURL = new URL("http://i5.nyu.edu/~cmk380/cs101/USStates.txt");

    // connect and obtain data from this URL
    Scanner input = new Scanner( serviceURL.openStream() );
    Scanner input1 = new Scanner(System.in);

    while(true)
    {
      boolean found = false;

      //Ask for state
      System.out.print("Enter state (abbreviation): ");
      String stateAbbreviation = input1.next();

      // read in our data
      while ( input.hasNext() )
      {
        // grab a line
        String data = input.nextLine();

        // split up the line based on the position of the commas
        String[] splitData = data.split(",");

        // grab out the abbreviation
        String abbreviation = splitData[1];  

        if(stateAbbreviation.equals(abbreviation))
        {
          state = splitData[0];
          abbrev = stateAbbreviation;
          found = true;
          break;
        }
      } 

      if (found == true)
      {
        // close our URL
        input.close();
        break;
      }
    }

    double numLines = 0;    

    // define our URL
    URL senatorURL = new URL("http://www.contactingthecongress.org/downloadsContactingCongress.db.txt");

    // connect and obtain data from this URL
    Scanner input2 = new Scanner( senatorURL.openStream() );

    // read in our data
    while ( input2.hasNext() )
    {
      // grab a line
      String data = input2.nextLine();

      if (numLines != 0)
      {
        // split up the line based on the position of the commas
        String[] splitData = data.split("\t");


        if(splitData[0].equals(abbrev+"SR"))
        {
          System.out.println("");
          System.out.println("State: " + state);
          System.out.println("");
          System.out.println("Senior Senator");
          System.out.println("Name: " + splitData[1]);

          //Try catch their information (exception raised if not found)
          try
          {
            System.out.println("Address: " + splitData[3]);
          }
          catch (Exception e)
          {
            System.out.println("Addrress unavailable.");
          }

          try
          {
            System.out.println("Phone: " + splitData[4]);
          }
          catch (Exception e)
          {
            System.out.println("Phone unavailable.");
          }

          try
          {
             System.out.println("Website: " + splitData[7]);
          }
          catch (Exception e)
          {
            System.out.println("Website unavailable.");
          }
          System.out.println("");
        }           

        if(splitData[0].equals(abbrev+"JR"))
        {
          System.out.println("Junior Senator");
          System.out.println("Name: " + splitData[1]);

          //Try catch their information (exception raised if not found)
          try
          {
            System.out.println("Address: " + splitData[3]);
          }
          catch (Exception e)
          {
            System.out.println("Addrress unavailable.");
          }

          try
          {
            System.out.println("Phone: " + splitData[4]);
          }
          catch (Exception e)
          {
            System.out.println("Phone unavailable.");
          }

          try
          {
            System.out.println("Website: " + splitData[7]);
          }
          catch (Exception e)
          {
            System.out.println("Website unavailable.");
          }
          System.out.println("");
        }

      }

      numLines ++; 
    }

    // close our URL
    input2.close();
  }
}
4

3 に答える 3

3

あなたは2つ持っていwhileて、それから抜け出すために休憩を使おうとします、しかしあなたはただ内側から抜け出しているだけですwhile

両方から抜け出すにはwhile、プログラムロジックを変更するか、ラベルを使用する必要があります(はい、Javaはラベルを使用でき、内側のループと外側のループから一度に切り離すことがその用途の1つです)

于 2012-12-10T18:56:29.200 に答える
1

これを行う別の方法は、

        public static boolean checkAbbreive(){
              while(true)
                {
                    boolean found = false;

                    //Ask for state
                    System.out.print("Enter state (abbreviation): ");
                    String stateAbbreviation = input1.next();

                    // read in our data
                    while ( input.hasNext() )
                    {
                        // grab a line
                        String data = input.nextLine();

                        // split up the line based on the position of the commas
                        String[] splitData = data.split(",");

                        // grab out the abbreviation
                        String abbreviation = splitData[1];  

                        if(stateAbbreviation.equals(abbreviation))
                        {
                            state = splitData[0];
                            abbrev = stateAbbreviation;
                            found = true;
                            return found;
                        }

               }
         if (checkAbbreive() == true)
            {
            // close our URL
            input.close();
            break;
            }

    }
}

while ループが関数に囲まれていることと、found = true の後に return found コマンドがあることに注意してください。

于 2012-12-10T18:59:21.907 に答える
0

これが機能するかどうかはわかりませんが、両方のループから抜け出すようにしてください。

breakTheWholeWhileLoop:
while(true)
{
   //For this version, we won't need the boolean found 
   //since we'll break out of everything

   //Ask for state
   System.out.print("Enter state (abbreviation): ");
   String stateAbbreviation = input1.next();

   //Read in our data
   while(input.hasNextLine() )
   {
      //...

      if(stateAbbreviation.equals(abbreviation) )
      {
         state = splitData[0];
         abbrev = stateAbbreviation;
         input.close();

         //This piece of code will break out of the while(true) loop
         break breakTheWholeWhileLoop;
      }
   } //while input loop
} //while true loop

//Do the rest

うまくいけば、それは役に立ちます。

于 2012-12-10T18:59:58.367 に答える