java - Java无限循环

标签 java loops input infinite

首先这是一个家庭作业问题。我完成了程序,并且运行良好;但我有一个小问题。该程序应该首先在线读取 txt 文件,并收集所有 50 个州及其缩写。接下来,我必须要求用户输入有效的州缩写。一旦他们这样做了,我就必须在线阅读另一个 txt 文件,并返回两位参议员的州信息。

我遇到的问题是我想循环扫描仪要求用户输入状态。如果他们输入了错误的状态,那么我想重新询问他们。现在,如果我进入合法状态,程序就可以正常工作。如果我进入了错误的状态,程序就会循环,并重新要求我进入另一个状态。如果我输入了错误的状态,然后输入了正确的状态,程序会要求我输入另一个状态,而不是继续执行程序。有什么帮助吗?

我认为问题是当程序循环时,它不会读取:

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

同样,让 find 始终 = 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();
  }
}

最佳答案

您有两个 while,然后您尝试使用中断来摆脱它,但您只是摆脱了内部 while

要摆脱这两个 while ,您需要更改程序逻辑或使用标签(是的,Java 可以使用标签,并立即从内部循环和外部循环中中断是它的用途之一)

关于java - Java无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13806880/

相关文章:

java - 日期的唯一值

java - 使用 Firestore 集合中的图像填充 RecyclerView

Java删除字符串问题(请不要使用StringBuilder或BufferedWriter给出任何解决方案)

python - python中的c风格循环

javascript - 如何动态更改输入数字的值并验证其中的数据?

java - 将太大的值转换为 java Short

java - Swift AES 实现

javascript - 如何在 JavaScript 中使用循环将十六进制转换为十进制

Javascript Regexp - 不接受空格但它应该

html - 为什么不垂直对齐 : middle work with input elements in a table-cell?