Java 如何使用用户输入存储多维数组中的数组
是的,这是我的家庭作业。但我已经被困了好几天了。 (可怕的老师,可怕的教训。这个老师永远不会对我或任何人的任何事情做出回应) 我以前在这里看到过类似的东西,但从未见过或理解过他们的答案 在这个程序中,用户输入一个状态,然后输出鸟和花的状态。在用户键入none之后,它应该给他们一个他们在一行中键入的所有内容的摘要。州、鸟、花。 我不知道该怎么做。。。你不必告诉我怎么做,那很好。但是你能不能用一种不含糊的方式引导我哈哈Java 如何使用用户输入存储多维数组中的数组,java,Java,是的,这是我的家庭作业。但我已经被困了好几天了。 (可怕的老师,可怕的教训。这个老师永远不会对我或任何人的任何事情做出回应) 我以前在这里看到过类似的东西,但从未见过或理解过他们的答案 在这个程序中,用户输入一个状态,然后输出鸟和花的状态。在用户键入none之后,它应该给他们一个他们在一行中键入的所有内容的摘要。州、鸟、花。 我不知道该怎么做。。。你不必告诉我怎么做,那很好。但是你能不能用一种不含糊的方式引导我哈哈 import java.util.Scanner; public class
import java.util.Scanner;
public class TheStatesTester {
public static void main(String[] args) {
String theState [][] = {
{"Alabama", "Northern Flicker","Camelia"},
{"Alaska", "Willow PTarmigan","ForgetMeNot"},
{"Arizona", "Cactus Wren","Saguaro Cactus Blossom"},
{"Arkansas", "Mockingbird","Apple Blossom"},
{"California", "California Quail","California Blossom"},
{"Colorado", "Lark Bunting","Rocking Mountain Columbine"},
{"Connecticut", "American Robin","Mountain Laurel"},
{"Delaware", "Blue Hen Chicken","Peach Blossom"},
{"Flordia", "Mockingbird","Orange Blossom"},
{"Georgia", "Brown Thrasher","Cherokee Rose"},
{"Hawaii", "Nene","Hibiscus"},
{"Idaho", "Peregrine Falcon","Mock Orange"},
{"Illinois", " Northern Cardinal","Purple Violet"},
{"Indiana", "Northern Cardinal","Peony"},
{"Iowa", "Eastern Goldrinch","Wild Prairie Rose"},
{"Kansas", "Western Meadowlark","Sunflower"},
{"Kentucky", "Northen Cardinal","Golden Rod"},
{"Louisiana", "Brown Pelican","Magnolia"},
{"Maine", "Black-capped Chickadee","White Pine Tassel and Cone"},
{"Maryland", "Baltimore Oriole","Black-Eyes Susan"},
{"Massachusetts", "Black-Capped Chickadee","Mayflower"},
{"Michigan", "Robin Redbreast","Apple Blossom"},
{"Minnesota", "Common Loon","Pink and White ladyslipper"},
{"Mississippi", "Wood Duck","Magnolia"},
{"Missouri", "Eastern Bluebird","Hawthorn"},
{"Montana", "Western Meadowlark","Bitterroot"},
{"Nebraska", "Western Medowlark","Goldenrod"},
{"Nevada", "Mountain Bluebird","Sagebush"},
{"New Hampshire", "Purple Finch","Purple Lilac"},
{"New Jersey", "Eastern Goldfinch","Violet"},
{"New Mexico", "Roadrunner","Yucca"},
{"New York", "Bluebird","Rose"},
{"North Carolina", "Cardinal","Flowering Dogwood"},
{"North Dakota", "Western Meadowlark","Wild Prairie Rose"},
{"Ohio", "Cardinal","Scarlet Carnation"},
{"Oklahoma", "Scissor-tailed Flycatcher","Mistletoe"},
{"Oregon", "Western Medowlark","Orange Grape"},
{"Pennsylvania", "Ruffed Grouse","Mountain Laurel"},
{"Rhode Island", "Rhode Island Red","Violet"},
{"South Carolina", "Great Carolina Wren","Yellow Jessamine"},
{"South Dakota", "Ring-necked Pheasant","Pasque Flower"},
{"Tennessee", "Mocking Bird","Iris"},
{"Texas", "Mocking Bird","Texas Bluebonnet"},
{"Utah", "Common American Gull","Sego Lily"},
{"Vermont", "Hermit Thrush","Red Clover"},
{"Virginia", "Cardinal","Flowering Dogwood"},
{"Washington", "Willow Goldfinch","Coast Rhodoendron"},
{"West Virginia", "Cardinal","Rhododendron"},
{"Wisconsin", "Robin","Violet"},
{"Wyoming", "Western Medowlark","Indian Paintbrush"},
};
Scanner input1 = new Scanner(System.in);
while (true) {
System.out.println("Enter a State or None to exit: ");
String states = input1.nextLine();
int position=getBirdFlower(theState, states);
if (states.trim().equalsIgnoreCase("None")){
System.out.println("**** Thank you *****\r\n" +
"A summary report for each State, Bird, and Flower is: " );
break;
}
else {
if(position!=-1)
{
System.out.println("Bird: "+theState[position][1]);
System.out.println("Flower: "+theState[position][2]);
}
else
System.out.println("Invalid state entered");
// for (int i=0; i<state.length;i)
// for (int j=0;j<state[i].length;j)
// System.out.println(state[i][j]);
}
}
}
public static int getBirdFlower(String theState[][], String state)
{
int position = -1;
boolean found=false;
for (int index = 0; index < theState.length && !found; index++)
{
if(theState[index][0].equalsIgnoreCase(state))
position = index;
}
return position;
}
}
import java.util.Scanner;
公共类国家元首{
公共静态void main(字符串[]args){
字符串状态[][]={
{“阿拉巴马”,“北方闪烁”,“骆驼花”},
{“阿拉斯加州”、“柳树公园”、“遗忘之门”},
{“亚利桑那州”、“仙人掌鹪鹩”、“仙人掌之花”},
{“阿肯色州”、“模仿鸟”、“苹果花”},
{“加州”、“加州鹌鹑”、“加州花”},
{“科罗拉多”,“百灵鸟旗”,“摇摆山科伦拜恩”},
{“康涅狄格州”、“美国知更鸟”、“山月桂”},
{“特拉华”,“蓝母鸡”,“桃花”},
{“Flordia”,“Mockingbird”,“Orange Blossom”},
{“乔治亚州”、“布朗·斯拉舍”、“切罗基玫瑰”},
{“夏威夷”、“奈恩”、“芙蓉”},
{“爱达荷州”、“游隼”、“仿橙”},
{“伊利诺伊州”、“北方红衣主教”、“紫紫罗兰”},
{“印第安纳”,“北方红衣主教”,“牡丹”},
{“爱荷华州”、“东部金林奇”、“野生草原玫瑰”},
{“堪萨斯州”、“西部草地云雀”、“向日葵”},
{“肯塔基州”,“北方红衣主教”,“金棒”},
{“路易斯安那州”、“褐鹈鹕”、“木兰花”},
{“缅因州”、“黑冠山雀”、“白松树流苏和圆锥体”},
{“马里兰”,“巴尔的摩黄鹂”,“黑眼睛苏珊”},
{“马萨诸塞州”、“黑冠山雀”、“五月花”},
{“密歇根”,“罗宾红胸”,“苹果花”},
{“明尼苏达州”、“普通九龙”、“粉色和白色的利珀夫人”},
{“密西西比”,“木鸭子”,“木兰花”},
{“密苏里”、“东方蓝鸟”、“山楂”},
{“蒙大拿州”、“西部草地云雀”、“比特罗特”},
{“内布拉斯加州”,“西部医疗方舟”,“黄花”},
{“内华达州”,“山蓝鸟”,“塞奇布什”},
{“新罕布什尔州”,“紫雀”,“紫丁香”},
{“新泽西”,“东金翅雀”,“紫罗兰”},
{“新墨西哥州”、“路行者”、“尤卡”},
{“纽约”、“蓝鸟”、“玫瑰”},
{“北卡罗来纳州”、“红衣主教”、“开花山茱萸”},
{“北达科他州”、“西部草地云雀”、“野生草原玫瑰”},
{“俄亥俄州”、“红衣主教”、“猩红康乃馨”},
{“俄克拉荷马州”、“剪尾捕蝇草”、“槲寄生”},
{“俄勒冈州”、“西部医疗方舟”、“橙葡萄”},
{“宾夕法尼亚州”、“皱褶松鸡”、“山月桂”},
{“罗德岛”、“罗德岛红”、“紫罗兰”},
{“南卡罗莱纳州”、“大卡罗莱纳鹪鹩”、“黄色杰西敏”},
{“南达科他州”、“环颈雉”、“帕斯克花”},
{“田纳西州”、“模仿鸟”、“虹膜”},
{“得克萨斯州”、“嘲鸟”、“得克萨斯蓝凤”},
{“犹他州”、“普通美国海鸥”、“塞戈百合”},
{“佛蒙特州”、“隐士画眉鸟”、“红三叶草”},
{“弗吉尼亚”,“红衣主教”,“开花山茱萸”},
{“华盛顿”,“柳树金翅雀”,“海岸罗多恩德隆”},
{“西弗吉尼亚州”、“红衣主教”、“杜鹃花”},
{“威斯康星州”、“罗宾”、“紫罗兰”},
{“怀俄明州”、“西部医疗方舟”、“印度画笔”},
};
扫描仪输入1=新扫描仪(System.in);
while(true){
System.out.println(“输入状态或无以退出:”);
字符串状态=input1.nextLine();
int位置=getBirdFlower(状态,状态);
if(states.trim().equalsIgnoreCase(“无”)){
System.out.println(“*****谢谢******\r\n”+
每个州、鸟和花的摘要报告如下:);
打破
}
否则{
如果(位置!=-1)
{
System.out.println(“Bird:+state[position][1]);
System.out.println(“Flower:+theState[position][2]);
}
其他的
System.out.println(“输入的状态无效”);
//对于(int i=0;i我在代码中添加了一些注释,以便清楚地理解。请仔细阅读
import java.util.ArrayList;
import java.util.Scanner;
public class TheStatesTester {
public static void main(String[] args) {
String theState[][] = {
{"Alabama", "Northern Flicker", "Camelia"},
{"Alaska", "Willow PTarmigan", "ForgetMeNot"},
{"Arizona", "Cactus Wren", "Saguaro Cactus Blossom"},
{"Arkansas", "Mockingbird", "Apple Blossom"},
{"California", "California Quail", "California Blossom"},
{"Colorado", "Lark Bunting", "Rocking Mountain Columbine"},
{"Connecticut", "American Robin", "Mountain Laurel"},
{"Delaware", "Blue Hen Chicken", "Peach Blossom"},
{"Flordia", "Mockingbird", "Orange Blossom"},
{"Georgia", "Brown Thrasher", "Cherokee Rose"},
{"Hawaii", "Nene", "Hibiscus"},
{"Idaho", "Peregrine Falcon", "Mock Orange"},
{"Illinois", " Northern Cardinal", "Purple Violet"},
{"Indiana", "Northern Cardinal", "Peony"},
{"Iowa", "Eastern Goldrinch", "Wild Prairie Rose"},
{"Kansas", "Western Meadowlark", "Sunflower"},
{"Kentucky", "Northen Cardinal", "Golden Rod"},
{"Louisiana", "Brown Pelican", "Magnolia"},
{"Maine", "Black-capped Chickadee", "White Pine Tassel and Cone"},
{"Maryland", "Baltimore Oriole", "Black-Eyes Susan"},
{"Massachusetts", "Black-Capped Chickadee", "Mayflower"},
{"Michigan", "Robin Redbreast", "Apple Blossom"},
{"Minnesota", "Common Loon", "Pink and White ladyslipper"},
{"Mississippi", "Wood Duck", "Magnolia"},
{"Missouri", "Eastern Bluebird", "Hawthorn"},
{"Montana", "Western Meadowlark", "Bitterroot"},
{"Nebraska", "Western Medowlark", "Goldenrod"},
{"Nevada", "Mountain Bluebird", "Sagebush"},
{"New Hampshire", "Purple Finch", "Purple Lilac"},
{"New Jersey", "Eastern Goldfinch", "Violet"},
{"New Mexico", "Roadrunner", "Yucca"},
{"New York", "Bluebird", "Rose"},
{"North Carolina", "Cardinal", "Flowering Dogwood"},
{"North Dakota", "Western Meadowlark", "Wild Prairie Rose"},
{"Ohio", "Cardinal", "Scarlet Carnation"},
{"Oklahoma", "Scissor-tailed Flycatcher", "Mistletoe"},
{"Oregon", "Western Medowlark", "Orange Grape"},
{"Pennsylvania", "Ruffed Grouse", "Mountain Laurel"},
{"Rhode Island", "Rhode Island Red", "Violet"},
{"South Carolina", "Great Carolina Wren", "Yellow Jessamine"},
{"South Dakota", "Ring-necked Pheasant", "Pasque Flower"},
{"Tennessee", "Mocking Bird", "Iris"},
{"Texas", "Mocking Bird", "Texas Bluebonnet"},
{"Utah", "Common American Gull", "Sego Lily"},
{"Vermont", "Hermit Thrush", "Red Clover"},
{"Virginia", "Cardinal", "Flowering Dogwood"},
{"Washington", "Willow Goldfinch", "Coast Rhodoendron"},
{"West Virginia", "Cardinal", "Rhododendron"},
{"Wisconsin", "Robin", "Violet"},
{"Wyoming", "Western Medowlark", "Indian Paintbrush"},
};
Scanner input1 = new Scanner(System.in);
ArrayList<String> userInputStates = new ArrayList<>();
while (true) {
System.out.println("Enter a State or None to exit: ");
String states = input1.nextLine();
if (states.trim().equalsIgnoreCase("None")) {
System.out.println("**** Thank you *****\r\n" +
"A summary report for each State, Bird, and Flower is: ");
//printing the userList
printUserList(userInputStates);
break;
} else {
//I moved this getBirdFlower method call to heere, because if not none , call from here
int position = getBirdFlower(theState, states);
if (position != -1) {
System.out.println("Bird: " + theState[position][1]);
System.out.println("Flower: " + theState[position][2]);
//Add user's found properties to arrayList.
userInputStates.add("- State: " + theState[position][0] + " Bird: " + theState[position][1] + " Flower: " + theState[position][2]);
} else
System.out.println("Invalid state entered");
}
}
}
private static void printUserList(ArrayList<String> userInputStates) {
//print userInputStates.
for (String userState : userInputStates) {
System.out.println(userState);
}
}
public static int getBirdFlower(String theState[][], String state) {
//you dont need to keep boolean , if found -> return current index
for (int index = 0; index < theState.length; index++) {
if (theState[index][0].equalsIgnoreCase(state))
return index;
}
//if not found, return -1
return -1;
}
}
诀窍是在用户输入时存储它。在这种情况下,我通过使用ArrayList简化了我的工作,但这也可以用普通数组来完成,但是管理数组需要更多的工作。我还添加了一些注释来解释每一步
public class TheStatesTester {
public static void main(String[] args) {
String theStates[][] = { { "Alabama", "Northern Flicker", "Camelia" },
{ "Alaska", "Willow PTarmigan", "ForgetMeNot" }, { "Arizona", "Cactus Wren", "Saguaro Cactus Blossom" },
{ "Arkansas", "Mockingbird", "Apple Blossom" },
{ "California", "California Quail", "California Blossom" },
{ "Colorado", "Lark Bunting", "Rocking Mountain Columbine" },
{ "Connecticut", "American Robin", "Mountain Laurel" },
{ "Delaware", "Blue Hen Chicken", "Peach Blossom" }, { "Flordia", "Mockingbird", "Orange Blossom" },
{ "Georgia", "Brown Thrasher", "Cherokee Rose" }, { "Hawaii", "Nene", "Hibiscus" },
{ "Idaho", "Peregrine Falcon", "Mock Orange" }, { "Illinois", " Northern Cardinal", "Purple Violet" },
{ "Indiana", "Northern Cardinal", "Peony" }, { "Iowa", "Eastern Goldrinch", "Wild Prairie Rose" },
{ "Kansas", "Western Meadowlark", "Sunflower" }, { "Kentucky", "Northen Cardinal", "Golden Rod" },
{ "Louisiana", "Brown Pelican", "Magnolia" },
{ "Maine", "Black-capped Chickadee", "White Pine Tassel and Cone" },
{ "Maryland", "Baltimore Oriole", "Black-Eyes Susan" },
{ "Massachusetts", "Black-Capped Chickadee", "Mayflower" },
{ "Michigan", "Robin Redbreast", "Apple Blossom" },
{ "Minnesota", "Common Loon", "Pink and White ladyslipper" },
{ "Mississippi", "Wood Duck", "Magnolia" }, { "Missouri", "Eastern Bluebird", "Hawthorn" },
{ "Montana", "Western Meadowlark", "Bitterroot" }, { "Nebraska", "Western Medowlark", "Goldenrod" },
{ "Nevada", "Mountain Bluebird", "Sagebush" }, { "New Hampshire", "Purple Finch", "Purple Lilac" },
{ "New Jersey", "Eastern Goldfinch", "Violet" }, { "New Mexico", "Roadrunner", "Yucca" },
{ "New York", "Bluebird", "Rose" }, { "North Carolina", "Cardinal", "Flowering Dogwood" },
{ "North Dakota", "Western Meadowlark", "Wild Prairie Rose" },
{ "Ohio", "Cardinal", "Scarlet Carnation" }, { "Oklahoma", "Scissor-tailed Flycatcher", "Mistletoe" },
{ "Oregon", "Western Medowlark", "Orange Grape" },
{ "Pennsylvania", "Ruffed Grouse", "Mountain Laurel" },
{ "Rhode Island", "Rhode Island Red", "Violet" },
{ "South Carolina", "Great Carolina Wren", "Yellow Jessamine" },
{ "South Dakota", "Ring-necked Pheasant", "Pasque Flower" }, { "Tennessee", "Mocking Bird", "Iris" },
{ "Texas", "Mocking Bird", "Texas Bluebonnet" }, { "Utah", "Common American Gull", "Sego Lily" },
{ "Vermont", "Hermit Thrush", "Red Clover" }, { "Virginia", "Cardinal", "Flowering Dogwood" },
{ "Washington", "Willow Goldfinch", "Coast Rhodoendron" },
{ "West Virginia", "Cardinal", "Rhododendron" }, { "Wisconsin", "Robin", "Violet" },
{ "Wyoming", "Western Medowlark", "Indian Paintbrush" },
};
Scanner scanner = new Scanner(System.in);
ArrayList<String> visitedStates = new ArrayList<>();//using this to store the states already asked.
while (true) {//loop forever... i'm not a fan of this
System.out.println("Enter a State or None to exit: ");
String state = scanner.nextLine().trim();//remove extra spaces and set the state
if ("None".equalsIgnoreCase(state)) {//check if user is done
System.out
.println("**** Thank you *****\r\n" + "A summary report for each State, Bird, and Flower is: ");
for(String vistedState : visitedStates) {//loop though all the visited states
int stateIndex = getStateIndex(theStates, vistedState);//find the index of the visited states
System.out.println(Arrays.toString(theStates[stateIndex]));//print out the info for the state
}
break;//exit the while loop
} else {//if the user is not done
int position = getStateIndex(theStates, state);//find the state index
if (position != -1) {//make sure it's a valid input
System.out.println("Bird: " + theStates[position][1]);//print out the bird
System.out.println("Flower: " + theStates[position][2]);//print out the flower
visitedStates.add(state);//add it to the list of visited states
} else {
System.out.println("Invalid state entered");//bad input we don't print and don't store
}
}
}
}
public static int getStateIndex(String theState[][], String state) {
int position = -1;
boolean found = false;
for (int index = 0; index < theState.length && !found; index++) {
if (theState[index][0].equalsIgnoreCase(state))
position = index;
}
return position;
}
}
当然,我不介意帮你。既然你正在努力学习,你能不能先格式化你的代码,然后尽可能多地添加注释,说明你认为每一行正在做什么以及你希望做什么。也许可以试着解释一下,如果你有一堆写有州名、鸟和花的卡片,我给你一个州名,你会怎么做。重要的一点是f函数getBirdFlower()
应该重命名,因为它不返回鸟和花的数据,而是数据的索引。提示:found
只设置一次(因此它不能更改为true
),但实际上根本不需要该变量;可以在一个函数中放置多个return
s(如果有意义的话),也在for循环中,该循环随后在返回时退出。请指定最终摘要应包含的内容:关于所有已知状态的数据、用户输入的所有状态的数据或仅用户输入的最后一个状态的数据?@michael butscher我已将其替换为findIndexLocation。我已删除找到的布尔值:)有点道理。我会玩的。哦,夏天的寿星
public class TheStatesTester {
public static void main(String[] args) {
String theStates[][] = { { "Alabama", "Northern Flicker", "Camelia" },
{ "Alaska", "Willow PTarmigan", "ForgetMeNot" }, { "Arizona", "Cactus Wren", "Saguaro Cactus Blossom" },
{ "Arkansas", "Mockingbird", "Apple Blossom" },
{ "California", "California Quail", "California Blossom" },
{ "Colorado", "Lark Bunting", "Rocking Mountain Columbine" },
{ "Connecticut", "American Robin", "Mountain Laurel" },
{ "Delaware", "Blue Hen Chicken", "Peach Blossom" }, { "Flordia", "Mockingbird", "Orange Blossom" },
{ "Georgia", "Brown Thrasher", "Cherokee Rose" }, { "Hawaii", "Nene", "Hibiscus" },
{ "Idaho", "Peregrine Falcon", "Mock Orange" }, { "Illinois", " Northern Cardinal", "Purple Violet" },
{ "Indiana", "Northern Cardinal", "Peony" }, { "Iowa", "Eastern Goldrinch", "Wild Prairie Rose" },
{ "Kansas", "Western Meadowlark", "Sunflower" }, { "Kentucky", "Northen Cardinal", "Golden Rod" },
{ "Louisiana", "Brown Pelican", "Magnolia" },
{ "Maine", "Black-capped Chickadee", "White Pine Tassel and Cone" },
{ "Maryland", "Baltimore Oriole", "Black-Eyes Susan" },
{ "Massachusetts", "Black-Capped Chickadee", "Mayflower" },
{ "Michigan", "Robin Redbreast", "Apple Blossom" },
{ "Minnesota", "Common Loon", "Pink and White ladyslipper" },
{ "Mississippi", "Wood Duck", "Magnolia" }, { "Missouri", "Eastern Bluebird", "Hawthorn" },
{ "Montana", "Western Meadowlark", "Bitterroot" }, { "Nebraska", "Western Medowlark", "Goldenrod" },
{ "Nevada", "Mountain Bluebird", "Sagebush" }, { "New Hampshire", "Purple Finch", "Purple Lilac" },
{ "New Jersey", "Eastern Goldfinch", "Violet" }, { "New Mexico", "Roadrunner", "Yucca" },
{ "New York", "Bluebird", "Rose" }, { "North Carolina", "Cardinal", "Flowering Dogwood" },
{ "North Dakota", "Western Meadowlark", "Wild Prairie Rose" },
{ "Ohio", "Cardinal", "Scarlet Carnation" }, { "Oklahoma", "Scissor-tailed Flycatcher", "Mistletoe" },
{ "Oregon", "Western Medowlark", "Orange Grape" },
{ "Pennsylvania", "Ruffed Grouse", "Mountain Laurel" },
{ "Rhode Island", "Rhode Island Red", "Violet" },
{ "South Carolina", "Great Carolina Wren", "Yellow Jessamine" },
{ "South Dakota", "Ring-necked Pheasant", "Pasque Flower" }, { "Tennessee", "Mocking Bird", "Iris" },
{ "Texas", "Mocking Bird", "Texas Bluebonnet" }, { "Utah", "Common American Gull", "Sego Lily" },
{ "Vermont", "Hermit Thrush", "Red Clover" }, { "Virginia", "Cardinal", "Flowering Dogwood" },
{ "Washington", "Willow Goldfinch", "Coast Rhodoendron" },
{ "West Virginia", "Cardinal", "Rhododendron" }, { "Wisconsin", "Robin", "Violet" },
{ "Wyoming", "Western Medowlark", "Indian Paintbrush" },
};
Scanner scanner = new Scanner(System.in);
ArrayList<String> visitedStates = new ArrayList<>();//using this to store the states already asked.
while (true) {//loop forever... i'm not a fan of this
System.out.println("Enter a State or None to exit: ");
String state = scanner.nextLine().trim();//remove extra spaces and set the state
if ("None".equalsIgnoreCase(state)) {//check if user is done
System.out
.println("**** Thank you *****\r\n" + "A summary report for each State, Bird, and Flower is: ");
for(String vistedState : visitedStates) {//loop though all the visited states
int stateIndex = getStateIndex(theStates, vistedState);//find the index of the visited states
System.out.println(Arrays.toString(theStates[stateIndex]));//print out the info for the state
}
break;//exit the while loop
} else {//if the user is not done
int position = getStateIndex(theStates, state);//find the state index
if (position != -1) {//make sure it's a valid input
System.out.println("Bird: " + theStates[position][1]);//print out the bird
System.out.println("Flower: " + theStates[position][2]);//print out the flower
visitedStates.add(state);//add it to the list of visited states
} else {
System.out.println("Invalid state entered");//bad input we don't print and don't store
}
}
}
}
public static int getStateIndex(String theState[][], String state) {
int position = -1;
boolean found = false;
for (int index = 0; index < theState.length && !found; index++) {
if (theState[index][0].equalsIgnoreCase(state))
position = index;
}
return position;
}
}
Enter a State or None to exit:
Idaho
Bird: Peregrine Falcon
Flower: Mock Orange
Enter a State or None to exit:
Nevada
Bird: Mountain Bluebird
Flower: Sagebush
Enter a State or None to exit:
None
**** Thank you *****
A summary report for each State, Bird, and Flower is:
[Idaho, Peregrine Falcon, Mock Orange]
[Nevada, Mountain Bluebird, Sagebush]