[英]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;
}
}
}
}
此輸出的一行示例如下:
1-紅酒--2----6.25-----12.50
我希望我的代碼保持迭代直到循環終止。 在Python中,這非常簡單,因為我可以使用索引在一個while循環下一次簡單地同時迭代所有對象。 不幸的是,因為這是我實際上第一天使用Java為類編寫代碼,所以我不禁感到迷茫,並認為它具有一套完全不同的規則,我尚無法掌握。
基本上,我只需要知道如何遍歷不同類型的多個數組列表並將它們打印在一行上即可。
順便說一句,很抱歉,但是我不知道如何在該網站上正確設置帖子的格式,這就是為什么我使用水平線來表示代表代碼“%d和%s”的空格的原因。 我總是收到有人在編輯我的帖子的通知,感覺我在沒有機會阻止它發生的情況下受到批評。 如果有人可以引導我到可以顯示我如何正確格式化代碼的地方,我將不勝感激。
我注釋掉一些內容來理解和解決您的問題。 你可以看到下面的代碼;
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.