[英]How do I take all of the user inputs in a while loop and put them into a print statement
[英]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 個項目。 然后該程序將所有項目編號與其名稱相對應地打印出來,並一一打印價格。 還計算所有項目的總數。 我的代碼只打印最后一個項目編號、名稱、價格和總計。 我不知道如何合並數組來做。 例如它應該如何看:
_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);
}
您需要將每個輸入存儲在數組或集合中,例如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 + ",");
}
我終於解決了這個任務。 感謝所有幫助過的人,真的很感激。 這是代碼:
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);
}
}
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.