[英]Print a value from 2d array in Java
我正在开发一个程序,允许用户将值添加到2d数组,然后搜索数组并显示值。 信息已正确存储,但是我只能显示动物名称而不是食物。 在被烤之前,我已经搜索并实现了许多不同的方法来尝试获得正确的输出。 我敢肯定,如果有人可以帮助我理解我的错误,那就非常简单了,谢谢!
/*This program will allow a user to enter information into the zoo
or search by animal for the type of food it eats*/
import java.util.Scanner;
class zoo {
//create array
static String[][] animalFood;
String[][] addArray(int x) {
animalFood = new String[x][2];
Scanner in = new Scanner(System.in);
//loop through array and add amount of items user chose
for (int row = 0; row < animalFood.length; row++){
System.out.print("Enter an animal name: ");
animalFood[row][0] = in.nextLine();
System.out.print("Enter the food the animal eats: ");
animalFood[row][1] = in.nextLine();
}
System.out.println("Thank you for adding information to the zoo!");
System.out.println("You entered the following information: ");
//loop through and print the informationa added
for(int i = 0; i < animalFood.length; i++)
{
for(int j = 0; j < animalFood[i].length; j++)
{
System.out.print(animalFood[i][j]);
if(j < animalFood[i].length - 1) System.out.print(" - ");
}
System.out.println();
}
//prompt the user to search or quit
System.out.println("Please enter the name of the animal to search for or Q to quit: ");
String animalName = in.nextLine();
animalName = animalName.toUpperCase();
if(animalName.equals("Q")){
System.out.println("Thanks for using the program!");
}
else {
searchArray(animalName);
}
return animalFood;
}
String[][] searchArray(String name) {
String matchResult = "There was no " + name + " found in the zoo!";
String itemToMatch = name.toUpperCase();
String arrayItem = "";
String food = "";
for (int i = 0; i < animalFood.length; i++) {
for (int j = 0; j < animalFood.length; j++) {
arrayItem = animalFood[i][j];
arrayItem = arrayItem.toUpperCase();
if(arrayItem.equals(itemToMatch)){
matchResult = "The animal " + name + " was found in the zoo! It eats " + animalFood[j];
}
else {
//nothing found
}
}
}
System.out.println(matchResult);
if (food != null) {
System.out.println(food);
}
return animalFood;
}
//constructor
public zoo() {
}
//overloaded constructor
public zoo(int x) {
int number = x;
animalFood = addArray(x);
}
//method to get users choice
public static int menu() {
int selection;
Scanner input = new Scanner(System.in);
System.out.println("Please make a choice in the menu below");
System.out.println("-------------------------\n");
System.out.println("1 - Add animals and the food they eat.");
System.out.println("2 - Search for an animal in the zoo.");
System.out.println("3 - Exit the program");
selection = input.nextInt();
return selection;
}
//main method
public static void main(String[] args) {
//create a new object
zoo myZoo = new zoo();
//variables and scanner
int userChoice;
int numberAnimals;
String animalName = "";
Scanner input = new Scanner(System.in);
//call the menu
userChoice = menu();
//actions based on user choice
if (userChoice == 1) {
System.out.println("How many animals would you like to enter information for?");
numberAnimals = input.nextInt();
myZoo.addArray(numberAnimals);
}
if (userChoice == 2) {
System.out.println("Please enter the name of the animal to search for: ");
animalName = input.nextLine();
myZoo.searchArray(animalName);
}
if (userChoice == 3) {
System.out.println("Thank you for using the program!");
}
}
}
在我看来,你的问题在于searchArray
。 您的嵌套for
循环迭代超过数组的一个维度的大小:
for (int i = 0; i < animalFood.length; i++) {
for (int j = 0; j < animalFood.length; j++) {
...
}
}
用animalFood[i].length
替换animalFood.length
,就像你在addArray
方法中正确使用addArray
。
编辑
它看起来也像你的输出方法不正确。
matchResult = "The animal " + name + " was found in the zoo! It eats " + animalFood[j];
在这一行中, animalFood[j]
应该是animalFood[i][j]
。 您看到的奇怪输出是Java尝试将数组转换为String。
第二次编辑
在检查了addArray
方法之后,似乎我对你的数组做了一个不正确的假设。 看起来您的数组的结构使得每个索引都有2个项目,即动物及其食物。 所以它看起来像这样:
animalFood [0] [0] ='猫'
animalFood [0] [1] ='猫食'
animalFood [1] [0] ='狗'
animalFood [1] [1] ='狗粮'
等等
如果是这种情况,那么您将需要更改循环以仅对外部索引进行迭代。 这意味着删除searchArray
内部的for循环。 然后,你只是将内部数组的第一个索引与你想要匹配的项目进行比较,如果匹配,那么食物将是第二个索引。 我会把实现留给你(因为这看起来像是一个家庭作业问题)。 如果我在这里说的话听起来不对,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.