最喜欢的食物Java程序出错了



下面是我的Java程序。现在,程序正确运行的唯一方法是使最后一列最长。如果数组中的最后一列不是最长的,则输出不正确。例如,如果food数组在第二列中的元素比在第三列中的元素多,如下所示:

String[][] food = {
{"Bannana", "Apple", "Pear", "Orange"}, // fruits
{"GreenBean", "Iceburg", "Spenach", "peas", "carrots", "potatoes", "beans"}, // vegetables 
{"Steak",   "Baccon", "Beef", "TurkeyB", "TurkeyBacon", "Chicken"} // meats 
};

然后,例如,如果输入chicken作为输入,则输出如下:

Yo have no favorite food
Your favorite food is Chicken

而且,例如,如果输入是biscuit,则输出是这样的:

Yo have no favorite food
Yo have no favorite food

那么,有没有办法只打印出"Yo have no favorite food"一次或正确打印出最喜欢的食物,而最后一列必须是最长的?

这是代码:

package com.begg;
import java.util.*;
public class List {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
String[][] food = {
{"Bannana", "Apple", "Pear", "Orange"}, // fruits
{"GreenBean", "Iceburg", "Spenach", "peas"}, // vegetables 
{"Steak",   "Baccon", "Beef", "TurkeyB", "TurkeyBacon", "Chicken"} // meats 
};
for(int row = 0; row < food.length; row++) {
for (int col = 0; col < food[row].length; col ++) {
System.out.print(food[row][col] + ", ");
}
System.out.println();
}
System.out.println("Enter your favorite out of the options above:");
String k = sc.next();
loop2:
for(int row2 = 0; row2<food.length; row2++) {
for (int col2 = 0; col2< food[row2].length; col2++) {
if (k.equalsIgnoreCase(food[row2][col2])) {
System.out.println("Your favorite food is " + food[row2][col2]);
break loop2;
}
else if (!(k.equalsIgnoreCase(food[row2][col2])) & col2== 5 ) {
System.out.println("Yo have no favorite food");
}
}
}
}
}

谢谢!

更改此部分:

else if (!(k.equalsIgnoreCase(food[row2][col2])) & col2== 5 ) {
System.out.println("Yo have no favorite food");
}

对此:

else if (!(k.equalsIgnoreCase(food[row2][col2])) && row2 == food.length-1 && col2 == food[row2].length-1) {
System.out.println("Yo have no favorite food");
}

应该做我认为你正在寻找的。编辑后的else if检查数组的所有值是否已检查(无论哪列更长(,如果没有与输入匹配,那么它将执行一次System.out.println("Yo have no favorite food");

使用哈希集而不是两个for来迭代2d array。代码类似 ;

import java.util.HashSet;
import java.util.Scanner;
public class List {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
String[][] food = {
{"Bannana", "Apple", "Pear", "Orange"}, // fruits
{"GreenBean", "Iceburg", "Spenach", "peas"}, // vegetables
{"Steak", "Baccon", "Beef", "TurkeyB", "TurkeyBacon", "Chicken"} // meats
/*
* Your longest part of your array should be the longest
* if it's not, using the length  of the column to end a loop may cause problems
* if you use the row instead, everything after the row three will execute
*
*/

};
for (int row = 0; row < food.length; row++) {
for (int col = 0; col < food[row].length; col++) {
System.out.print(food[row][col] + ", ");
}
System.out.println();
}
System.out.println("Enter your favorite out of the options above:");
String k = sc.next();
final HashSet<Object> setOfFood = new HashSet<>();
//Add all food to hashset
for (String[] subFoodList : food) {
for (String subFood : subFoodList) {
setOfFood.add(subFood.toLowerCase());
}
}
//if set containts scanners string returns relevant response
if (setOfFood.contains(k.toLowerCase())) {
System.out.println("Your favorite food is " + k);
} else {
System.out.println("Yo have no favorite food");
}
}
}

相关内容

  • 没有找到相关文章

最新更新