gpt4 book ai didi

Java:如何将用户输入分配给数组

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:23 27 4
gpt4 key购买 nike

所以我正在制作一个java程序,它接受客户购买的东西,并使用数组和 HashMap 来计算CSE作业的成本(也用它来打印其他东西)。但现在我希望用户能够首先输入产品,然后输入数量,以便能够制作一个简单的、用户友好的“收银机”程序。基本上只是想要 system.out 对如何做到这一点有什么想法吗?

不确定包含多个 Scanner/system.in 代码行的循环是否有效,而且我真的不确定如何解决这个问题。

哦,如果有人问类似的问题,请告诉我!我找不到类似的东西,但我可能错了。

import java.util.HashMap;
import java.util.ArrayList;
public class preLab3 {

// PreLab Q1: Compute the price of buying a specific quantity of a single item

public static double getPriceSingleItemType(HashMap<String, Double> priceList, String itemType, int quantity) {
double itemPrice = priceList.get(itemType);
return itemPrice * quantity;
}


// PreLab Q2: Compute the price of a single order
public static double getPrice(HashMap<String, Double> priceList, ArrayList<String> basket) {

double totalCost = 0.0;
for(String item: basket) {
double itemPrice = priceList.get(item);
totalCost = totalCost + itemPrice;
}

return totalCost;
}


public static HashMap<String, Double> getPriceList() {
HashMap<String, Double> priceList = new HashMap<String, Double>();

priceList.put("eggs", 1.79); // per dozen
priceList.put("butter", 3.19);
priceList.put("cheese", 5.99);
priceList.put("apple", 0.99); // per pound
priceList.put("banana", 0.39); // per pound
priceList.put("strawberries", 1.99);
priceList.put("peppers", 0.99);
priceList.put("tomato sauce", 0.5);
priceList.put("chocolate chips", 2.29);

return priceList;
}
public static void main(String[] args) {

System.out.println(getPriceList());

// PreLab Q3: I want to buy 3 lbs of strawberries
double price = getPriceSingleItemType(getPriceList(), "strawberries", 3);
System.out.println(price);

ArrayList<String> cart = new ArrayList<>();

cart.add("strawberries");
cart.add("strawberries");
cart.add("strawberries");
cart.add("chocolate chips");
cart.add("banana");
cart.add("banana");
cart.add("apple");
cart.add("eggs");
cart.add("butter");

double totalCost = getPrice(getPriceList(), cart);
System.out.println("The customer must pay: " + totalCost + " USD");

ArrayList<HashMap<String, Integer>> orders = new ArrayList<>();
for(HashMap<String, Integer> maps : orders) {


}
}

}

最佳答案

首先创建一个Scanner用于用户输入的对象,然后使用您已获得的示例代码,其中 cart.add(item) 添加用户输入 item,并循环每个项目以获取用户输入 quantity。总的来说是这样的:

// ... same code above   
ArrayList<String> cart = new ArrayList<>();

Scanner input = new Scanner(System.in);
String user_item = "";
int user_quantity = 0;
System.out.println("What items would you like to buy?");

// Loop to repeatedly get user input and add items to cart
// until user enters "q" for item to quit.
do {
System.out.print("Item? (q to quit) ");
user_item = input.nextLine();
if (user_item.equals("q")){
break;
}
System.out.print("How many " + user_item + "? ");
user_quantity = input.nextInt();
System.out.println(user_quantity + " " + user_item);
for(int q = 0; q <= user_quantity; q++){
cart.add(user_item);
}
} while(!input.nextLine().equals("q"));
input.close();

// Remove these
// cart.add("strawberries");
// cart.add("strawberries");
// cart.add("strawberries");
// cart.add("chocolate chips");
// cart.add("banana");
// cart.add("banana");
// cart.add("apple");
// cart.add("eggs");
// cart.add("butter");

double totalCost = getPrice(getPriceList(), cart);
// ... same code below

运行示例:

{banana=0.39, eggs=1.79, apple=0.99, butter=3.19, peppers=0.99, chocolate chips=2.29, tomato sauce=0.5, strawberries=1.99, cheese=5.99}
What items would you like to buy?
Item? (q to quit) butter
How many butter? 1
1 butter
Item? (q to quit) eggs
How many eggs? 2
2 eggs
Item? (q to quit) q
The customer must pay: 11.75 USD

当然,您可能还想为无效的用户输入添加错误/异常处理。

希望这有帮助。

关于Java:如何将用户输入分配给数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48836146/

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