首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在数组中找到最大值双精度

问如何在数组中找到最大值双精度
EN

Stack Overflow用户
提问于 2021-10-20 23:54:28
回答 1查看 57关注 0票数 0

我在做一个计算器来计算医用大麻药房的价格。所有的销售都要征收16%的税,以及在某些重量下的价格优惠。我的目标是找到一个权重,在考虑到税收和优惠的情况下,以客户的最佳价位出售。我正在尝试获取doubles列表中的最高值,与断点数组进行比较,看看它们是否满足断点的特定阈值。我真的不能在weightList数组中获得最大的双精度。这是我的代码。我的双倍数gramPrice、eighthPrice等是给定断点处的价格/克。gramWeight、eighthWeight,等是在断点处计算接收乘积的权重。我觉得术语有点草率,但是我怎么才能找到双精度数组的最大值呢?

代码语言:javascript
复制
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};

            
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-21 00:09:48

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

使用标准Java 8+

代码语言:javascript
复制
final double maxWeight = Arrays.stream(weightList).max().getAsDouble();

使用其他库

Apache Commons Lang3的NumberUtils

代码语言:javascript
复制
final double maxWeight = NumberUtils.max(weightList);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69654288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档