[英]Java: How to assign user input to array
So I'm making a java program that takes in what a customer bought and uses an array and hash map to figure out how much it cost for CSE homework(was messing around with printing other things with this as well). 因此,我正在制作一个Java程序,该程序接收客户购买的商品,并使用数组和哈希图来计算CSE作业的成本(也正在与此同时打印其他内容)。 But now I want to be able for the user to just input firstly the product and then the quantity to be able to make a simple, user friendly "cash-register" program. 但是现在我希望用户能够先输入产品,然后输入数量,以制作简单,用户友好的“收款机”程序。 Basically just want system.out's Any thoughts on how to do this? 基本上只需要system.out的有关如何执行此操作的想法?
Not sure if a loop with multiple scanner/system.in lines of code would work or not, and I'm really unsure on how to tackle this problem. 不确定使用多个scan / system.in代码行的循环是否可以工作,我不确定如何解决此问题。
Oh and also if something similar was asked please tell me! 哦,如果有人提出类似的要求,请告诉我! I wasn't able to find anything similar, but I'm probably mistaken. 我找不到类似的东西,但我可能错了。
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) {
}
}
} }
First off you should consider looking at this post for future/current reference on static methods. 首先,您应该考虑看一下这篇文章,以获取有关静态方法的将来/当前参考。 https://stackoverflow.com/a/7084473/1770303 https://stackoverflow.com/a/7084473/1770303
In this case you should be referencing your hashmap as an object either by declaring it globally in your class and adding key value pairs in a method or creating a separate class that does essentially the same thing. 在这种情况下,您应该通过在类中全局声明它并在方法中添加键值对或创建一个本质上相同的单独类来将哈希图引用为对象。 How you organize you code largely depends on the individual. 您如何组织代码在很大程度上取决于个人。 Lastly, each user input should be checked against the key values to see if its a valid key before continuing. 最后,在继续操作之前,应对照键值检查每个用户输入,以查看其是否为有效键。 If the user input is not contained in your map what is the requirement? 如果用户输入未包含在您的地图中,那么要求是什么? How will you handle that? 您将如何处理?
As for the scanner i would reference the following post which provides the link to documentation on the scanner class. 至于扫描仪,我将参考以下文章,该文章提供了有关扫描仪类文档的链接。 https://stackoverflow.com/a/14679497/1770303 https://stackoverflow.com/a/14679497/1770303
First create a Scanner
object for user input, then use the example code you've already got where cart.add(item)
adds the user input item
, and loop each item for user input quantity
. 首先创建一个用于用户输入的Scanner
对象,然后使用您已经获得的示例代码,其中cart.add(item)
添加用户输入item
,并循环每个用户输入quantity
。 So altogether something like this: 所以总共是这样的:
// ... 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
Example run: 示例运行:
{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
Of course you probably also want to add error/exception handling for invalid user input. 当然,您可能还想为无效的用户输入添加错误/异常处理。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.