首页 > 解决方案 > 我不知道如何关联 2 个数组之间的位置

问题描述

我一直在做一个检查输入命令的程序,如果它进入菜单,将存储在一个列表中。现在我必须将每个订单与他们在菜单上的位置联系起来,以关联他们的正确价格,我不知道该怎么做......所以我们有 menu[q,w,e,r,t] 和价格 [ 1,2,3,4],如果你输入“t”,程序必须知道它的价格是 4,而不是 0 或 1,因为逻辑数组索引。感谢所有人,并为我糟糕的英语和代码知识感到抱歉!

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Fase2_final {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String[] menu = { "chicken", "steak", "hamburger", "spaghettis", "pizza" };
        int[] price = { 10, 15, 20, 5, 12 };

        for (int i = 0; i < menu.length; i++) {

            System.out.println("We have " + menu[i] + " for " + price[i] + "€");

        }

        int option = 0;
        int yes = 1;
        int no = 0;

        System.out.println("What do you want to eat?");

        List<String> Order = new ArrayList<String>();

        List<Integer> TotalPrice = new ArrayList<Integer>();

        Scanner keyboard = new Scanner(System.in);

        do {
            String userOrder = keyboard.next().toLowerCase();

            boolean found = false;

            for (int j = 0; j < menu.length; j++) {

                if (userOrder.equals(menu[j])) {
                    found = true;

                    Order.add(userOrder);

                    System.out.println(
                            "You ordered " + userOrder + "\nSomething more?" + "\nSay YES with 1 and NO      with 0");

                    option = keyboard.nextInt();

                    keyboard.nextLine();

                    break;
                }
            }

            if (!found) {
                System.out.println(
                        "We don't have this on menu!" + "\nDo you want to order another thing that we do have in menu?"
                                + "\nSay YES with 1 and NO with 0");

                option = keyboard.nextInt();

                keyboard.nextLine();
            }

        } while (option == yes);

        //HOW CAN THE PROGRAM KNOWS THAT USERINPUTS SHOULD HAVE AN ORDER IN RELATION TO THE MENU AT THE SAME AS THE PRICES

        // Need to add the prices of the user's orders to a TotalPrice list
        //First we want to add just the prices related to the number of orders
        for (int w = 0; w < Order.size(); w++) {

            /* Then i don't know how to add just the correct prices of user's orders related to their positions in
                menu[i] and prices[i]*/
            for (int z = 0; z < Order.size(); z++) {
                TotalPrice.add(Order.indexOf(menu[z]));
            }
        }

        System.out.println("Order finished! \nYour order have " + Order);

        System.out.println("The price is: " + TotalPrice);

        keyboard.close();

    }
}

标签: javaarrays

解决方案


您想将菜单项映射到价格。在这种情况下,您应该使用Map<String, Double>,因为与数组相比,它更加有效和方便(在生产中,您永远不会将数组用于此类任务)。String key - 菜单项的名称,Double value - 价格。

像这样声明(顺便说一下,我建议使用以小写字母开头的名称来声明类对象)

Map<String, Double> menu = new HashMap<>();
Double totalPrice = 0;
menu.put("chicken", 10); // The example of adding element to the map, add another items to menu like that
order.add("chicken");

for (int z = 0; z < Order.size(); z++)
    totalPrice += menu.get(order.get(z)); // + 5

阅读有关地图界面的更多信息。


推荐阅读