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

时间:2018-09-02 20:17:11

标签: java arrays for-loop if-statement user-input

下面是我的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");
                }
            }
        }

    }
}

谢谢!

2 个答案:

答案 0 :(得分:0)

使用哈希表而不是两个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");
        }

    }
}

答案 1 :(得分:0)

更改此部分:

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");