Java 如何确定输入的输入值最多
我很难确定哪种产品类型输入最多。例如,一个人可以输入“水”、“水”、“咖啡”和“牛奶”。我的预期产出将是“水是订单最多的产品”。这是我的主线逻辑。有人能帮忙吗Java 如何确定输入的输入值最多,java,Java,我很难确定哪种产品类型输入最多。例如,一个人可以输入“水”、“水”、“咖啡”和“牛奶”。我的预期产出将是“水是订单最多的产品”。这是我的主线逻辑。有人能帮忙吗 public static void main(String[] args) { final int MAX_GUESTS = 16; final int MAX_DRINKS = 48; double[] drinkCosts = new double[MAX_DRINKS]; in
public static void main(String[] args) {
final int MAX_GUESTS = 16;
final int MAX_DRINKS = 48;
double[] drinkCosts = new double[MAX_DRINKS];
int count = 0;
String productType = getProductType();
while (!productType.equals("-1")) {
if (count < MAX_GUESTS) {
count++;
String productVariation = getProductVariation(productType);
for (int i = 0; i < count; i++) {
drinkCosts[count] = getDrinkCost(productVariation);
}
}
else {
JOptionPane.showMessageDialog(null, "Come back tomorrow.");
}
productType = getProductType();
}
double total = getTotal(drinkCosts);
print(total);
}
publicstaticvoidmain(字符串[]args){
最终积分最大值=16;
最终整数最大值=48;
double[]drinkCosts=新的double[MAX_饮料];
整数计数=0;
字符串productType=getProductType();
而(!productType.equals(“-1”)){
如果(计数<最大客人数){
计数++;
字符串productVariation=getProductVariation(productType);
for(int i=0;i
我建议使用。使用字符串
作为键类型(即产品类型
),使用整数
作为值类型(即该产品类型
的出现次数)
每次读入productType
(在while
循环开始时),检查productType
是否已经是地图中的一个键。如果是,则将其映射到的计数增加1。如果没有,请使用HashMap.put(字符串键,整数值)
方法将productType
添加到计数为1的映射中
在while
循环之后,只需在地图中循环检查输入最多的productType
(计数最高):
这些数组基本上是一个映射,其中字符串中的每个productType
都映射到counts
中相同索引处的计数(例如,productType[5]
已输入counts[5]
次)
然后,当您在while
循环中读取productType
时,循环查看productTypes
。如果找到该类型,则递增计数的相应索引(例如,如果productType
位于productTypes[5]
,则递增计数[5]
。如果不是(如果在找到输入的类型之前,在productTypes
中找到一个空的元素),将该元素设置为给定的productType
,并将计数的相应索引设置为1
然后,只需更改我给出的上述代码片段:
int highestCount = 0;
String mostEnteredProductType = null;
for (int i = 0; i < NUM_DRINKS; i++) {
// Once we reach a null productType, we have reached the end of the
// entered productTypes
if (productTypes[i] == null) {
break;
} else if (counts[i] > highestCount) {
highestCount = counts[i];
mostEnteredProductType = productTypes[i];
}
}
// Should probably check that mostEnteredProductType isn't null here.
System.out.println(mostEnteredProductType + " was the most ordered product.");
int highestCount=0;
字符串mostEnteredProductType=null;
对于(int i=0;i最高计数){
最高计数=计数[i];
mostEnteredProductType=productTypes[i];
}
}
//应该检查MostenterdProductType在这里是否不为null。
System.out.println(mostEnteredProductType+“是订单最多的产品”);
你知道如何只使用数组吗?@dan2693请查看上面对我答案的编辑。希望这会有所帮助,但如果你还有任何问题,请告诉我。@dan2693我知道你已经接受了我的答案,但这里还有一个建议:最好创建一个ProductTypeCount类,包含两个字段:productType和count。然后,您只需要一个ProductTypeCounts数组,而不是productTypes和counts的两个单独数组。
String[] productTypes = new String[MAX_DRINKS];
int[] counts = new int[MAX_DRINKS];
int highestCount = 0;
String mostEnteredProductType = null;
for (int i = 0; i < NUM_DRINKS; i++) {
// Once we reach a null productType, we have reached the end of the
// entered productTypes
if (productTypes[i] == null) {
break;
} else if (counts[i] > highestCount) {
highestCount = counts[i];
mostEnteredProductType = productTypes[i];
}
}
// Should probably check that mostEnteredProductType isn't null here.
System.out.println(mostEnteredProductType + " was the most ordered product.");