[英]How can I iterate over multiple arrays of different types in a loops?
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;
}
}
}
}
An example of what one line of this output would look like is this: 此输出的一行示例如下:
1 -- Red Wine -- -- 2 -- -- -- -- 6.25 -- -- -- -- -- 12.50
1-红酒--2----6.25-----12.50
I want my code to keep iterating until the loop terminates. 我希望我的代码保持迭代直到循环终止。 In Python this is extremely simple as I can simply iterate all the objects at the same time under one while-loop using the index.
在Python中,这非常简单,因为我可以使用索引在一个while循环下一次简单地同时迭代所有对象。 Unfortunately, as this is my first day actually coding in Java for one of my classes, I can't help but feel lost and think it has a completely different set of rules that I'm unable to grasp yet.
不幸的是,因为这是我实际上第一天使用Java为类编写代码,所以我不禁感到迷茫,并认为它具有一套完全不同的规则,我尚无法掌握。
Basically, I just need to know how I can iterate over multiple array lists of different types and print them on a single line. 基本上,我只需要知道如何遍历不同类型的多个数组列表并将它们打印在一行上即可。
By the way, I am sorry but I do not know how I can format my posts correctly on this website which is why I used the horizontal lines to indicate spaces that represent the "%d and %s" of my code. 顺便说一句,很抱歉,但是我不知道如何在该网站上正确设置帖子的格式,这就是为什么我使用水平线来表示代表代码“%d和%s”的空格的原因。 I always get notifications of someone editing my post and it feels like I am getting criticized without being provided an opportunity to prevent it from happening.
我总是收到有人在编辑我的帖子的通知,感觉我在没有机会阻止它发生的情况下受到批评。 If someone can direct me to somewhere that shows how I can properly format my code, I would greatly appreciate it.
如果有人可以引导我到可以显示我如何正确格式化代码的地方,我将不胜感激。
I comment out some lines to understand and solve your problem. 我注释掉一些内容来理解和解决您的问题。 U can see below code ;
你可以看到下面的代码;
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;
}
}
}
} }
This is the change I made 这是我所做的更改
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.