我在做一个计算器来计算医用大麻药房的价格。所有的销售都要征收16%的税,以及在某些重量下的价格优惠。我的目标是找到一个权重,在考虑到税收和优惠的情况下,以客户的最佳价位出售。我正在尝试获取doubles列表中的最高值,与断点数组进行比较,看看它们是否满足断点的特定阈值。我真的不能在weightList数组中获得最大的双精度。这是我的代码。我的双倍数gramPrice、eighthPrice等是给定断点处的价格/克。gramWeight、eighthWeight,等是在断点处计算接收乘积的权重。我觉得术语有点草率,但是我怎么才能找到双精度数组的最大值呢?
import java.util.Scanner;
import java.text.DecimalFormat;
public class main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("##.##");
int desiredPrice = 0 , pricePoint = 0;
int fivePerGram[] = {5, 15, 25, 50, 100};
int eightPerGram[] = {8, 25, 45, 90, 160};
double weightFormat[] = {1, 3.5, 7, 14, 28};
System.out.print("Enter Desired Amount OTD: ");
if(reader.hasNextInt() == true) {
desiredPrice = reader.nextInt();
}
System.out.print("Enter Price Point (3, 5, 8, 10, 12): " );
if(reader.hasNextInt() == true) {
pricePoint = reader.nextInt();
}
if(pricePoint == 8) {
double gramPrice = (eightPerGram[0]/1)*1.16;gramPrice = Double.parseDouble(df.format(gramPrice));
double eighthPrice = (eightPerGram[1]/3.5)*1.16;eighthPrice = Double.parseDouble(df.format(eighthPrice));
double quarterPrice = (eightPerGram[2]/7)*1.16;quarterPrice = Double.parseDouble(df.format(quarterPrice));
double halfPrice = (eightPerGram[3]/14)*1.16;halfPrice = Double.parseDouble(df.format(halfPrice));
double ouncePrice = (eightPerGram[4]/28)*1.16;ouncePrice = Double.parseDouble(df.format(ouncePrice));
double gramWeight = desiredPrice/gramPrice;gramWeight = Double.parseDouble(df.format(gramWeight));
double eighthWeight = desiredPrice/eighthPrice;eighthWeight = Double.parseDouble(df.format(eighthWeight));
double quarterWeight = desiredPrice/quarterPrice;quarterWeight = Double.parseDouble(df.format(quarterWeight));
double halfWeight = desiredPrice/halfPrice;halfWeight = Double.parseDouble(df.format(halfWeight));
double ounceWeight = desiredPrice/ouncePrice;ounceWeight = Double.parseDouble(df.format(ounceWeight));
double weightList[] = {gramWeight, eighthWeight, quarterWeight, halfWeight, ounceWeight};
}
}
}发布于 2021-10-21 00:09:48
注意:我很确定我知道你想要做什么。如果您想要weightList数组中的最高值,只需执行以下操作。
使用标准Java 8+
final double maxWeight = Arrays.stream(weightList).max().getAsDouble();使用其他库
Apache Commons Lang3的NumberUtils
final double maxWeight = NumberUtils.max(weightList);https://stackoverflow.com/questions/69654288
复制相似问题