package statescapitalquizz;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* @author Steve
*/
public class Statescapitalquizz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String fileName = "Capitals.txt";
boolean found = false;
Scanner kb = new Scanner(System.in);
String target;
int n = 50;
String[] states = new String[n];
String[] capitals = new String[n];
try (Scanner inputStream = new Scanner(new FileInputStream(fileName))) {
for (int i = 0; i < n; i++) {
states[i] = inputStream.nextLine();
capitals[i] = inputStream.nextLine();
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
System.out.println("Please enter the name of a state: ");
target = kb.nextLine();
// the loop coninues to the end of the array if the city is not found
for (n=0; (!found) && (n < states.length) ; n++) {
if (states[n].matches(target)) {
//print found message and set found to true
System.out.println("The capital of " + target + " is"); //Where I need help
found = true;
} // end if
} // for loop
// after the loop – if not(found) print not found message
if (!found)
System.out.println(target + "is not a state in the United States");
}
}
好的,我需要帮助的部分是如何将匹配的“大写”检索到用户输入的“状态”。我的输出语句基本上是
//print found message and set found to true
System.out.println("The capital of " + target + " is" + ); //Where I need help
found = true;
} // end if
} // for loop
我不知道在输出语句中放入什么代码,因此它交叉引用我的第二个数组,并将匹配的Capital放在用户输入的状态中。
答案 0 :(得分:1)
System.out.println("The capital of " + target + " is " + capitals[n]);
正如我在问题的评论中所提到的那样,答案就是答案。