如何在一个循环中遍历多个不同类型的数组?

时间:2018-09-09 07:47:58

标签: java loops arraylist

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

public class P8 {
    public static void main(String[] args) {
        double runningTotal = 0.00;
        double subTotal = 0.00;

        int d = 0;
        int d1 = 1;
        ArrayList<String> menuItems = new ArrayList<String>();
        ArrayList<Double> menuCost = new ArrayList<Double>();
        ArrayList<Integer> menuQuantity = new ArrayList<Integer>();
        int counter = 0;
        String[] options = {
            "Y", "y", "Yes", "yes", "YES"
        };

        boolean found = false;

        while(d < d1) {
            ++counter;
            Scanner order = new Scanner(System.in);
            System.out.print("What would you like to order? "); //item order
            String foodItem = order.nextLine();
            menuItems.add(foodItem);

            Scanner quantity = new Scanner(System.in);
            System.out.print("How many orders of that item would you like? "); //item order quantity 
            int foodQuantity = quantity.nextInt();
            menuQuantity.add(foodQuantity);

            double randNum = (double)(System.currentTimeMillis() % 11);
            menuCost.add(randNum);
            runningTotal = randNum * foodQuantity;
            subTotal += runningTotal;

            Scanner response = new Scanner(System.in);
            System.out.print("Would you like to order more items? ");
            String response1 = response.nextLine();


            for (String elements:options) {
                if (response1.equals(elements)){        
                    found = true;
                    break;
                }
            }


            if (found == true) {
                found = false;
                d = 0;  
            }


            else {
                int counter2 = 0;
                int counter3 = 1;

                while (counter2 <= counter) {

                    for (String elements:menuItems) {
                        double itemQuantity = menuQuantity.get(counter2);
                        double itemCost = menuCost.get(counter2);
                        System.out.printf("%2d%10s%5d%10.2f%10.2f\n", counter3, elements, itemQuantity, itemCost, (itemQuantity * itemCost));
                        counter2 += 1;
                        counter3 += 1;      
                    }
                }
                break;
            }

        }

    }
}

此输出的一行示例如下:

  

1-红酒--2----6.25-----12.50

我希望我的代码保持迭代直到循环终止。在Python中,这非常简单,因为我可以使用索引在一个while循环下一次简单地同时迭代所有对象。不幸的是,因为这是我实际上第一天使用Java为类编写代码,所以我不禁感到迷茫,并认为它具有一套完全不同的规则,我尚无法掌握。

基本上,我只需要知道如何遍历不同类型的多个数组列表并将它们打印在一行上即可。

对不起,很抱歉,但是我不知道如何在此网站上正确设置帖子的格式,这就是为什么我使用水平线来表示代表代码“%d和%s”的空格的原因。我总是收到有人在编辑我的帖子的通知,感觉我在没有机会阻止它发生的情况下受到批评。如果有人可以将我定向到可以显示如何正确设置代码格式的地方,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

我注释了一些内容以理解和解决您的问题。你可以看到下面的代码;

import java.util.ArrayList;
import java.util.Scanner;
public class P8 {

    public static void main(String[] args) {
        double runningTotal = 0.00;
        double subTotal = 0.00;

        int d = 0;
        int d1 = 1;
        ArrayList<String> menuItems = new ArrayList<String>();
        ArrayList<Double> menuCost = new ArrayList<Double>();
        ArrayList<Integer> menuQuantity = new ArrayList<Integer>();
        int counter = 0;
        String[] options = {
                "Y", "y", "Yes", "yes", "YES"
        };

        boolean found = false;

        while (d < d1) {
            ++counter;
            Scanner order = new Scanner(System.in);
            System.out.print("What would you like to order? "); //item order
            String foodItem = order.nextLine();
            menuItems.add(foodItem);

            Scanner quantity = new Scanner(System.in);
            System.out.print("How many orders of that item would you like? "); //item order quantity
            int foodQuantity = quantity.nextInt();
            menuQuantity.add(foodQuantity);

            double randNum = (double) (System.currentTimeMillis() % 11);
            menuCost.add(randNum);
            runningTotal = randNum * foodQuantity;
            subTotal += runningTotal;

            Scanner response = new Scanner(System.in);
            System.out.print("Would you like to order more items? ");
            String response1 = response.nextLine();


            for (String elements : options) {
                if (response1.equals(elements)) {
                    found = true;
                    break;
                }
            }


            if (found == false) {
                found = false;
                d = 0;
            } else {
                int counter2 = 0;
                int counter3 = 1;

                //i changed this with counter2+1 , because your logic as arraylist and starts from zero. you will get outofbound exception
                while (counter2 + 1 <= counter) {

                    for (String elements : menuItems) {
                        //do it itemquantity as integer , if u want double quantity , change.
                        int itemQuantity = menuQuantity.get(counter2);
                        double itemCost = menuCost.get(counter2);
                        //and here format is wrong in itemQuantity, if your itemQuantity param  is double write %2f , if integer write %5d
                        System.out.printf("%2d %10s %5d %10.2f %10.2f\n", counter3, elements, itemQuantity, itemCost, (itemQuantity * itemCost));
                        counter2 += 1;
                        counter3 += 1;
                    }
                }
                break;
            }

        }

    }

}

这是我所做的更改

if (found) {
    found = false;
    d = 0;
} else {
    int counter2 = 0;
    int counter3 = 1;
    while (counter2 < counter) {
        String menuItem = menuItems.get(counter2);
        int itemQuantity = menuQuantity.get(counter2);
        double itemCost = menuCost.get(counter2);
        System.out.printf("%2d%10s%5d%10.2f%10.2f\n", counter3, menuItem, itemQuantity, itemCost, (itemQuantity * itemCost));
        counter2 += 1;
        counter3 += 1;
        if (counter2 == counter) {
            System.out.printf("%2d%10s%12s%13.2f\n", counter3, "Subtotal:", ".....", subTotal);
            double i = subTotal * 0.08625;
            System.out.printf("%2d%10s%12s%13.2f\n", counter3 + 1, "Tax:", ".....", i);
            System.out.printf("%2d%10s%12s%13.2f\n", counter3 + 2, "Total:", ".....", (subTotal + i));
            break;
        }
    }
    break;
}