Java 我不知道';我不知道如何关联两个数组之间的位置

Java 我不知道';我不知道如何关联两个数组之间的位置,java,arrays,Java,Arrays,我一直在做一个程序,检查输入顺序,如果它进入菜单将存储在一个列表中。现在我必须把每一份订单和它们在菜单上的位置联系起来,才能把它们的正确价格联系起来,我不知道怎么做。。。 我们有菜单[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

我一直在做一个程序,检查输入顺序,如果它进入菜单将存储在一个列表中。现在我必须把每一份订单和它们在菜单上的位置联系起来,才能把它们的正确价格联系起来,我不知道怎么做。。。 我们有菜单[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();

    }
}

import java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公开课Fase2_期末考试{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
String[]菜单={“鸡肉”、“牛排”、“汉堡包”、“意大利面”、“比萨饼”};
int[]价格={10,15,20,5,12};
对于(int i=0;i
您希望将菜单项映射到价格。在这种情况下,您应该使用
映射
,因为与数组相比,它更有效、更方便(在生产中,您永远不会使用数组执行此类任务)。字符串键-菜单项的名称,双值-价格

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

Map菜单=新建HashMap();
双倍总价=0;
菜单。放入(“鸡肉”,10);//在向地图添加元素的示例中,向菜单添加另一项,如下所示
订单。添加(“鸡肉”);
对于(intz=0;z

阅读有关的详细信息。

您想将菜单项映射到价格。在这种情况下,您应该使用
映射
,因为与数组相比,它更有效、更方便(在生产中,您永远不会使用数组执行此类任务)。字符串键-菜单项的名称,双值-价格

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

Map菜单=新建HashMap();
双倍总价=0;
菜单。放入(“鸡肉”,10);//在向地图添加元素的示例中,向菜单添加另一项,如下所示
订单。添加(“鸡肉”);
对于(intz=0;z

阅读有关的详细信息。

创建一个包含食物和价格变量的简单类,并在数组中使用该类,您就不会遇到此问题。@Jaykooh您应该使用Java标准库提供的
Map
集合。我用下面的例子给出了一个答案,请告诉我它是否帮助您创建了一个包含食物和价格变量的简单类,并在数组中使用该类,您不会遇到这个问题。@Jaykooh您应该使用Java标准库提供的
Map
collection。我贴了一个下面的例子,请让我知道,如果它帮助你
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