首先這是一個作業問題。我完成了程序,並且運行良好;但我有一個小問題。該程序應該首先在線閱讀一個txt文件,並收集所有50個州及其縮寫。接下來,我必須要求用戶輸入一個有效的州名縮寫。一旦他們這樣做了,我必須在線閱讀另一個txt文件,然後讓兩位參議員回到他們的狀態。無限循環Java
我遇到的問題是我想循環掃描儀,要求用戶輸入一個狀態。如果他們進入不正確的狀態,那麼我想重新問他們。現在,如果我進入合法狀態,程序運行良好。如果我輸入的狀態不正確,程序將循環,並重新要求我進入另一個狀態。如果我進入一個不正確的狀態,然後進入正確的狀態,程序要求我進入另一個狀態,而不是繼續執行程序。請幫忙嗎?
我認爲這個問題是程序循環時,它不讀取:
if(stateAbbreviation.equals(abbreviation))
{
state = splitData[0];
abbrev = stateAbbreviation;
found = true;
break;
}
再次,使發現總是= 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();
}
}
你說在新的編輯中它總是返回false。你是說現在只返回false還是隻在false和true後返回false? – DrinkJavaCodeJava
檢查while循環中的if語句是否通過將System.out.println(「If executed」);如果沒有,我想我可能會知道你的問題。 – DrinkJavaCodeJava