gpt4 book ai didi

java - 如何在循环中迭代多个不同类型的数组?

转载 作者:行者123 更新时间:2023-12-02 10:50:06 26 4
gpt4 key购买 nike

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 -- Red Wine -- -- 2 -- -- -- -- 6.25 -- -- -- -- -- 12.50

我希望我的代码继续迭代,直到循环终止。在 Python 中,这非常简单,因为我可以使用索引在一个 while 循环下同时迭代所有对象。不幸的是,由于这是我第一天为我的一个类(class)实际使用 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;
}

关于java - 如何在循环中迭代多个不同类型的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52242431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com