如何利用存储在多维数组中的数据

问题描述 投票:0回答:1

如何显示用Java(Eclipse)存储在多维数组中的多个数据?我需要输出在每个位置记录的糖果,我不确定该怎么做。用户需要首先在每个位置输入带有价格的糖果。他们完成所有输入后,应该会输出一个菜单,其中包含他们在特定位置输入的糖果。我不明白的是如何显示存储输入的糖果的数组。我的代码可以在下面显示

import java.util.Scanner;
public class DessertExplosion {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        //Declare Variables
        String dessert[][][]=new String[3][3][2];
        double price[]=new double[18];
        int counter=0;
        String locationMenu="";
        //Print menu statement
        System.out.println("DESSERT EXPLOSION DAILY MENU ENTRY");
        //Use for loop to determine dessert types and prices
        for(int x=0;x<dessert.length;x++) {
            for(int n=0;n<dessert[x].length;n++) {
                for(int i=0;i<dessert[x][n].length;i++) {
                    counter++;
                    //Oshawa location
                    if (counter==1) {
                        System.out.println("---Oshawa---");
                        System.out.println("------------------------------");
                        //Cakes
                        System.out.println("Category: Cake");
                        System.out.println("Enter Cake item #1");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cake $");
                        price[i]=input.nextDouble();
                        System.out.println("Enter Cake item #2");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cake $");
                        price[i]=input.nextDouble();
                        System.out.println("------------------------------");
                        //Cookies
                        System.out.println("Category: Cookie");
                        System.out.println("Enter Cookie item #1");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");
                        price[i]=input.nextDouble();
                        System.out.println("Enter Cookie item #2");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");
                        price[i]=input.nextDouble();
                        System.out.println("------------------------------");
                        //Ice Cream
                        System.out.println("Category: Ice Crea,");
                        System.out.println("Enter Ice Cream item #1");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");
                        price[i]=input.nextDouble();
                        System.out.println("Enter Ice Cream item #2");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");
                        price[i]=input.nextDouble();
                    }
                    //Whitby Location
                    else if(counter==2) {
                        System.out.println("---Whitby---");
                        System.out.println("------------------------------");
                        //Cakes
                        System.out.println("Category: Cake");
                        System.out.println("Enter Cake item #1");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cake $");
                        price[i]=input.nextDouble();
                        System.out.println("Enter Cake item #2");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cake $");
                        price[i]=input.nextDouble();
                        System.out.println("------------------------------");
                        //Cookies
                        System.out.println("Category: Cookie");
                        System.out.println("Enter Cookie item #1");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");
                        price[i]=input.nextDouble();
                        System.out.println("Enter Cookie item #2");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");
                        price[i]=input.nextDouble();
                        System.out.println("------------------------------");
                        //Ice Cream
                        System.out.println("Category: Ice Crea,");
                        System.out.println("Enter Ice Cream item #1");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");
                        price[i]=input.nextDouble();
                        System.out.println("Enter Ice Cream item #2");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");
                        price[i]=input.nextDouble();
                    }
                    //Ajax location
                    else if(counter==3){
                        System.out.println("---Ajax---");
                        System.out.println("------------------------------");
                        //Cakes
                        System.out.println("Category: Cake");
                        System.out.println("Enter Cake item #1");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cake $");
                        price[i]=input.nextDouble();
                        System.out.println("Enter Cake item #2");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cake $");
                        price[i]=input.nextDouble();
                        System.out.println("------------------------------");
                        //Cookies
                        System.out.println("Category: Cookie");
                        System.out.println("Enter Cookie item #1");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");
                        price[i]=input.nextDouble();
                        System.out.println("Enter Cookie item #2");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");
                        price[i]=input.nextDouble();
                        System.out.println("------------------------------");
                        //Ice Cream
                        System.out.println("Category: Ice Cream");
                        System.out.println("Enter Ice Cream item #1");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");
                        price[i]=input.nextDouble();
                        System.out.println("Enter Ice Cream item #2");
                        dessert[x][n][i]=input.next();
                        System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");
                        price[i]=input.nextDouble();
                    }
                }
            }
        }

    }

}
java eclipse multidimensional-array
1个回答
0
投票

您可能会做基本相同的事情,即使用遍历多维数组的for循环,使用循环索引检索每个元素,然后将其打印出来。您可以在每个打印的甜点之间使用一个空格来输出全部内容。由于它是3维数组,因此其显示将取决于您要执行的操作。

我个人将其显示为二维数组,但是元素(在第三个数组中)直接显示为这样的数组。

Arrays.toString(desserts[x][n])

但这取决于您。

© www.soinside.com 2019 - 2024. All rights reserved.