gpt4 book ai didi

java - 添加重复字符串的数量

转载 作者:太空宇宙 更新时间:2023-11-04 14:35:05 25 4
gpt4 key购买 nike

我的任务是对类别列表进行排序。购物 list ,如果你愿意的话。这些类别是用户的输入、要购买的元素。输入相关类别的字符串名称(当然使用扫描仪)后,用户就可以输入该类别的数量(整数),然后输入该类别的单位成本( double )。系统会提示他们重复此操作,直到他们指定的类别为“end”。

这一切都很好,我已经编写了代码来获取所有这些信息,并查找并打印出最大的成本项目、最大数量的项目和其他信息。我需要帮助的是我的重复类别。例如,假设用户输入“cars”,后跟整数 3,然后输入数字 24000.00。然后他们输入“冰箱”,然后输入 1,然后输入 1300.00。然后用户输入第一个条目的副本,即“cars”,后跟整数 5,最后是 double 37000.00。如何让我的代码重新访问旧条目,将新数量添加到旧条目中,并存储该值而不覆盖旧条目?我还需要找到列表中项目的最大平均成本。我是 HashMap 新手,所以我在代码上遇到了困难://create an arrayList to store values

              // create an arrayList to store values

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

listOne.add("+ 1 item");

listOne.add("+ 1 item");

listOne.add("+ 1 item");



// create list two and store values

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

listTwo.add("+1 item");

listTwo.add("+1 item");



// put values into map

multiMap.put("some sort of user input detailing the name of the item", listOne);

multiMap.put("some other input detailing the name of the next item", listTwo);
// i need the user to input items until they type "end"

// Get a set of the entries

Set<Entry<String, ArrayList<String>>> setMap = multiMap.entrySet();

// time for an iterator
Iterator<Entry<String, ArrayList<String>>> iteratorMap = setMap.iterator();

System.out.println("\nHashMap with Multiple Values");

// display all the elements
while(iteratorMap.hasNext()) {

Map.Entry<String, ArrayList<String>> entry =

(Map.Entry<String, ArrayList<String>>) iteratorMap.next();

String key = entry.getKey();

List<String> values = entry.getValue();

System.out.println("Key = '" + key + "' has values: " + values);

}
// all that up there gives me this:

具有多个值的 HashMapKey =“详细说明下一项的名称的其他输入”具有值:[+1项,+1项]Key =“某种详细说明项目名称的用户输入”具有值:[+ 1 项目,+ 1 项目,+ 1 项目]

但我没有给用户机会输入商品数量或成本......我迷路了。

最佳答案

尝试这一小段示例代码,它包括一个 Main 类和两个依赖类,StatsPrinter 和 ShoppingEntry

package com.company;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main {
public static void main(String[] args) throws IOException {
String category;
String quantity;
String value;

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

HashMap<String, List<ShoppingEntry>> shoppingList = new HashMap<String, List<ShoppingEntry>>();

while(true) {
System.out.print("Enter the category of your item: ");
category = bufferedReader.readLine();

if("end".equals(category)){
break;
}

System.out.print("Enter the quantity of your item: ");
quantity = bufferedReader.readLine();

System.out.print("Enter the value of your item: ");
value = bufferedReader.readLine();

if (shoppingList.containsKey(category)) {
shoppingList.get(category).add(new ShoppingEntry(Integer.parseInt(quantity), Double.parseDouble(value)));
}else{
shoppingList.put(category, new ArrayList<ShoppingEntry>());
shoppingList.get(category).add(new ShoppingEntry(Integer.parseInt(quantity), Double.parseDouble(value)));
}
}

StatsPrinter.printStatistics(shoppingList);
}
}

和 ShoppingEntry 类

package com.company;

public class ShoppingEntry {
private int quantity;
private double price;

public ShoppingEntry(){
quantity = 0;
price = 0;
}

public ShoppingEntry(int quantity, double price){
this.quantity = quantity;
this.price = price;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
}

最后是 StatsPrinter 类,它利用 ShoppingEntry 的 HashMap 的数据结构来打印所需的统计信息

package com.company;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

public class StatsPrinter {
private static DecimalFormat format = new DecimalFormat("#.##");

public static void printStatistics(HashMap<String, List<ShoppingEntry>> shoppingList) {
printNuumberOfItems(shoppingList);
printLargestValue(shoppingList);
printLargestAverage(shoppingList);
}

private static void printNuumberOfItems(HashMap<String, List<ShoppingEntry>> shoppingList) {
System.out.println("There are " + shoppingList.keySet().size() + " items in your Shopping List");
}

private static void printLargestValue(HashMap<String, List<ShoppingEntry>> shoppingList) {
double currentLargestPrice = 0;
String largestPriceCategory = new String();

for(String keyValue : shoppingList.keySet()) {
for(ShoppingEntry entry : shoppingList.get(keyValue)) {
if (entry.getPrice() > currentLargestPrice) {
currentLargestPrice = entry.getPrice();
largestPriceCategory = keyValue;
}
}
}

System.out.println(largestPriceCategory + " has the largest value of: " + format.format(currentLargestPrice));
}

private static void printLargestAverage(HashMap<String, List<ShoppingEntry>> shoppingList) {
double currentLargestAverage = 0;
String largestAverageCategory = new String();
double totalCost = 0;
int numberOfItems = 0;

for(String keyValue : shoppingList.keySet()) {
for(ShoppingEntry entry : shoppingList.get(keyValue)) {
totalCost += entry.getPrice();
numberOfItems += entry.getQuantity();
}
if((totalCost / numberOfItems) > currentLargestAverage) {
currentLargestAverage = totalCost / numberOfItems;
largestAverageCategory = keyValue;
}
}

System.out.println(largestAverageCategory + " has the largest average value of: " + format.format(currentLargestAverage));
}
}

关于java - 添加重复字符串的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25691374/

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