无限循环Java [英] Infinite Loop Java

查看:107
本文介绍了无限循环Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

First of all this is a homework problem. I finished the program, and it runs fine; but I have a little side problem. The program is supposed to first read a txt file online, and gather all 50 states and their abbreviations. Next, I have to ask the user to enter in a valid state abbreviation. Once they do that, I have to read another txt file online, and return the two senators for their state.

我遇到的问题是我想循环扫描程序,要求用户输入状态.如果它们进入不正确的状态,那么我想重新询问它们.现在,如果我进入合法状态,则该程序可以正常运行.如果我输入的状态不正确,程序会循环运行,并再次询问我是否要进入另一状态.如果我进入错误状态,然后进入正确状态,则程序会要求我进入另一状态,而不是继续执行该程序.有什么帮助吗?

The problem I'm having is that I want to loop the scanner asking the user to enter a state. If they enter an incorrect state, then I want to re-ask them. Right now, if I enter in a legit state, the program works fine. If I enter in an incorrect state, the program loops, and re-asks me to enter in another state. If I enter in an incorrect state, and then enter in a correct state, the program asks me to enter in another state, rather than continuing with the program. Any help please?

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

I think the problem is that when the program loops, it doesn't read:

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

再次,使发现总是= false.

Again, making found always = 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离开

You have two while and then you try to use a break to get out of it, but you're just getting out of the inner while.

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

To get out of both whiles, you need to change your program logic or use labels (yes, Java can use labels, and to break from an inner loop and of the outter loop at once is one of it's uses)

这篇关于无限循环Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆