有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在数组中找到最大值加倍

我正在做一个计算器,来计算医用大麻药房的价格。对所有销售都征收16%的税,并且在一定的权重下有价格优惠。我的目标是找到一个权重,在给定税收和优惠的情况下,以最佳价格点为客户销售。我只想尝试获取一系列双精度的最大值,与断点数组进行比较,看看它们是否满足断点的特定阈值。实际上,我似乎无法在weightList数组中获得最大的double。这是我的密码。我的doublegramPriceeighthPrice等是给定断点处的价格/克。gramWeighteighthWeight,等是计算出的在该断点处接收的产品重量。我想这是一个有点草率的术语,但是我如何才能找到我的双数组的最大值呢

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};

            
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    注意:我很确定我知道你想做什么。如果您想要weightList数组中的最大值,只需执行以下操作

    使用标准Java8+

    final double maxWeight = Arrays.stream(weightList).max().getAsDouble();
    

    与其他图书馆合作

    Apache Commons Lang3的^{}

    final double maxWeight = NumberUtils.max(weightList);