提问人:Rashad F 提问时间:12/20/2022 更新时间:12/22/2022 访问量:327
如何将所有用户输入存储在 do while 循环中,并调用它们稍后在 Java 程序中打印?
How to store all the user inputs in do while loop, and call them to print later in Java program?
问:
我必须编写一个带有 do while 循环的 Java 程序。基本上,这就像杂货店的自助结账登记。首先程序提示用户输入项目 1 名称和价格,输入后会询问“是否要添加更多?”并提示用户。如果我们说是,那么它会重复,添加项目 2(项目编号增量)名称和价格。所以它一直这样,直到我们说“不”或直到输入 10 个项目。之后,程序会逐个打印所有项目编号、相应的名称和价格。还计算所有项目的总和。我的代码只打印最后一件商品编号、名称、价格和总数。我不知道如何合并数组来做while。示例它应该是什么样子的:
_output: Enter Item1 and its price:_
_input: Tomatoes_
_input: 5.5_
_output: Add one more item?_
_input: yes_
_output: Enter Item2 and its price:_
_input: Cheese_
_input: 3.5_
_output: Add one more item?_
_input: yes_
_output: Enter Item3 and its price:_
_input: Apples_
_input: 6.3_
_output: Add one more item?_
_input: no_
_output: Item1: Tomatoes Price: 5.5, Item2: Cheese Price: 3.5, Item3: Apples Price: 6.3_
_output: Total price: 15.3_
这是我的代码。它只打印最后一件商品的编号、名称和价格以及总数。但无法像示例一样一一打印所有内容。
Scanner scan = new Scanner(System.in);
String item;
double price;
int i=1;
double total=0;
String quest;
String items [] = {};
do {
System.out.println("Enter item " + i + " and it's price:");
item = scan.next();
price=scan.nextDouble();
i++;
total=total+price;
System.out.println("Add one more item?");
quest=scan.next();
} while (quest.equalsIgnoreCase("yes")) ; {
System.out.println("Item "+i+": " + item + " Price: " + price + ",");
System.out.println("Total price: " + total);
}
答:
0赞
Cardinal System
12/20/2022
#1
您需要将每个输入存储在数组或集合中,例如 ArrayList
。
如果选择使用数组,则可以询问用户将输入多少个项目,然后将该值用于数组的长度:
System.out.print("Enter the number of items in your cart: ");
int arraySize = scan.nextInt();
String[] itemNames = new String[arraySize];
double[] itemPrices = new double[arraySize];
如果您决定使用列表,则无需向用户询问列表大小,因为列表会动态扩展数据类型:
List<String> itemNames = new ArrayList<String>();
List<Double> itemPrices = new ArrayList<Double>();
然后,对于循环的每次迭代,将值保存到数组/列表中:
System.out.print("Enter the number of items in your cart: ");
int arraySize = scan.nextInt();
String[] itemNames = new String[arraySize];
double[] itemPrices = new double[arraySize];
for(int i = 0; i < ararySize; i++) {
System.out.println("Enter item " + (i + 1) + " and it's price:");
itemNames[i] = scan.next();
itemPrices[i] = scan.nextDouble();
// ...
}
// ...
或者对于列表方法:
List<String> itemNames = new ArrayList<String>();
List<Double> itemPrices = new ArrayList<Double>();
do {
System.out.println("Enter item " + i + " and it's price:");
itemNames.add(scan.next());
itemPrices.add(scan.nextDouble());
// ...
} while (quest.equalsIgnoreCase("yes")); {
// ...
}
然后,您可以遍历列表/数组以打印输出:
for(int i = 0; i < itemNames.length; i++) {
String name = itemNames[i];
double price = itemPrices[i];
System.out.println("Item " + i + ": " + name + " Price: " + price + ",");
}
对于列表:
for(int i = 0; i < itemNames.size(); i++) {
String name = itemNames.get(i);
double price = itemPrices.get(i);
System.out.println("Item " + i + ": " + name + " Price: " + price + ",");
}
0赞
Rashad F
12/22/2022
#2
我终于解决了任务。感谢所有帮助过的人,真的很感激。代码如下:
Scanner scan = new Scanner(System.in);
String itemName;
double price;
String[] items = new String[10];
double[] prices = new double[10];
int itemNumbers[] = new int[10];
String answer;
double total = 0;
int i = 1;
int index = 0;
do {
System.out.println("Enter item " + i + " and it's price:");
itemName = scan.next();
price = scan.nextDouble();
scan.nextLine();
total = total + price;
items[index] = itemName;
prices[index] = price;
itemNumbers[index] = i;
index++;
System.out.println("Add one more item?");
answer = scan.next();
i++;
} while (answer.equalsIgnoreCase("yes") && i <= 10); {
for (int j = 0; j < index; j++) {
System.out.print("Item " + itemNumbers[j] + ": " + items[j] + " price: " + prices[j] + ",");
}
}
System.out.println("\nTotal price: " + total);
-1赞
Rashad F
12/22/2022
#3
This version works too. Without arrays.
String shoppingList = "";
String item = "";
String addMore = "";
double price = 0;
int count = 1;
double totalPrice = 0;
do {
System.out.println("Enter Item"+ count + " and its price:");
item = scan.next();
price = scan.nextDouble();
shoppingList += ("Item" + count + ": " + item + " Price: " + price + ", ");
totalPrice += price;
System.out.println("Add one more item?");
addMore = scan.next();
count++;
} while (addMore.equalsIgnoreCase("yes"));
System.out.println(shoppingList.substring(0, shoppingList.length()-2));
System.out.println("Total price: " + totalPrice);
评论
1赞
Gauthier Tassin
12/23/2022
请改进您的格式答案。“此版本也有效”应该在代码块之外。你的代码块应该有很好的缩进和格式规则(你可以为此使用格式化程序)
评论