如何在数组中找到最大值 double
How to find the maximum value double in an array
我正在构建一个计算器来计算医用大麻药房的价格。所有销售均需缴纳 16% 的税,并在特定重量下享受价格优惠。我的目标是在给定税收和休息的情况下,为客户找到一个以最佳价格出售的重量。我只想尝试获取双精度列表的最高值,与断点数组进行比较以查看它们是否满足断点的特定阈值。从字面上看,我似乎无法在 weightList
数组中获得最大双倍数。这是我的代码。我的双打 gramPrice
、eighthPrice
等是给定断点处的 price/gram。 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};
}
}
}
请注意,我很确定我明白您要做什么。如果您想要 weightList
数组中的最高值,只需执行以下操作。
标准 Java 8+
final double maxWeight = Arrays.stream(weightList).max().getAsDouble();
与其他图书馆
Apache Commons Lang3 的 NumberUtils
final double maxWeight = NumberUtils.max(weightList);
我正在构建一个计算器来计算医用大麻药房的价格。所有销售均需缴纳 16% 的税,并在特定重量下享受价格优惠。我的目标是在给定税收和休息的情况下,为客户找到一个以最佳价格出售的重量。我只想尝试获取双精度列表的最高值,与断点数组进行比较以查看它们是否满足断点的特定阈值。从字面上看,我似乎无法在 weightList
数组中获得最大双倍数。这是我的代码。我的双打 gramPrice
、eighthPrice
等是给定断点处的 price/gram。 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};
}
}
}
请注意,我很确定我明白您要做什么。如果您想要 weightList
数组中的最高值,只需执行以下操作。
标准 Java 8+
final double maxWeight = Arrays.stream(weightList).max().getAsDouble();
与其他图书馆
Apache Commons Lang3 的 NumberUtils
final double maxWeight = NumberUtils.max(weightList);