Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用输入的设备成本找到一笔资金的最有效使用?_Java_Loops_Max - Fatal编程技术网

Java 如何使用输入的设备成本找到一笔资金的最有效使用?

Java 如何使用输入的设备成本找到一笔资金的最有效使用?,java,loops,max,Java,Loops,Max,我已经为学校做了几个小时的一个项目,但似乎还是做不好。我被指示为3个设备价格、总资金和剩余限额获取用户输入,然后计算出我可以购买的3个设备的金额,以使总成本尽可能接近最大资金,同时保持在限额内。程序必须使用某种嵌套循环。这就是我目前所拥有的绝对不起作用的东西: import java.text.DecimalFormat; import java.util.Scanner; public class GrandFund { public static void main(String[

我已经为学校做了几个小时的一个项目,但似乎还是做不好。我被指示为3个设备价格、总资金和剩余限额获取用户输入,然后计算出我可以购买的3个设备的金额,以使总成本尽可能接近最大资金,同时保持在限额内。程序必须使用某种嵌套循环。这就是我目前所拥有的绝对不起作用的东西:

import java.text.DecimalFormat;
import java.util.Scanner;
public class GrandFund {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        DecimalFormat fmt =  new DecimalFormat("#.##");

        int amt1 = 0, amt2 = 0, amt3 = 0;
        double price1, price2, price3, fund, limit, total1 = 0, total2 = 0, total3 = 0;

        System.out.println("Equipment #1 Price:");
        price1 = scan.nextDouble();

        System.out.println("Equipment #2 Price:");
        price2 = scan.nextDouble();

        System.out.println("Equipment #3 Price:");
        price3 = scan.nextDouble();

        System.out.println("Total Fund Amount:");
        fund = scan.nextDouble();

        System.out.println("Remaining Fund Limit:");
        limit = scan.nextDouble();

        while((fund - (total1 + total2 + total3)) <= limit) {
            for(amt1 = 0; price1 * amt1 <= fund; amt1++) {
                total1 = price1 * amt1;
                for(amt2 = 0; price2 * amt2 <= fund - total1; amt2++) {
                    total2 = price2 * amt2;
                    for(amt3 = 0; price3 * amt3 <= fund - total1 - total2; amt3++) {
                        total3 = price3 * amt3;
                    }
                }
            }
        }
        double remainder = fund - (total1 + total2 + total3);
        System.out.println(fmt.format(remainder) + ", " + fmt.format(amt1) + ", " + fmt.format(amt2) + ", " + fmt.format(amt3));
        scan.close();
    }
}
导入java.text.DecimalFormat;
导入java.util.Scanner;
公营级公积金{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
DecimalFormat fmt=新的DecimalFormat(“#.##”);
int amt1=0,amt2=0,amt3=0;
双倍价格1,价格2,价格3,基金,限额,总计1=0,总计2=0,总计3=0;
系统输出打印项次(“设备1价格:”;
price1=scan.nextDouble();
系统输出打印项次(“设备2价格:”;
price2=scan.nextDouble();
系统输出打印项次(“设备3价格:”;
price3=scan.nextDouble();
System.out.println(“资金总额:”);
fund=scan.nextDouble();
System.out.println(“剩余资金限额”);
limit=scan.nextDouble();

而((fund-(total1+total2+total3))除非您需要提高解决方案的效率,否则最简单的方法就是彻底搜索。即尝试所有组合并与“最佳”进行比较

如果创建一个嵌套类来存储“组合”,那么代码可能会更优雅一些:

private class Combination {
    public Combination(int amount1, int amount2, int amount3) {
        ...
    }

    public int getCost() {
        ...
    }
}
然后,您的试用代码变得简单得多:

Optional<Combination> best = Optional.empty();
for (int amount1 = ...) {
    for (int amount2 = ...) {
        for (int amount3 = ...) {
            Combination combo = new Combination(amount1, amount2, amount3);
            int cost = combo.getCost();
            if (cost < limit && (best.isEmpty() || cost > best.get().getCost()))
                best = Optional.of(combo);
        }
    }
}
Optional best=Optional.empty();
对于(int amount1=…){
对于(int amount2=…){
对于(int amount3=…){
组合组合=新组合(数量1、数量2、数量3);
int cost=combo.getCost();
if(costbest.get().getCost())
最佳=可选。of(组合);
}
}
}

这可以通过使用
Iterable
折叠嵌套for循环来改进,但这可能比您需要的更复杂。

在执行循环计算时,您需要选择更好的结果。主要逻辑是:

if (total1 + total2 + total3 > fund) {
    break;
}
//here is the code to pick the current better result
if (total1 + total2 + total3 <= fund
        && total1 + total2 + total3 >= fund - limit) {
    double currentCost = total1 + total2 + total3;
    if (currentCost > maxCost) {
        maxCost = currentCost;
        amountOfMaxCostOptional = Optional.of(new Amount(amt1, amt2, amt3));
    }
}
if(total1+total2+total3>基金){
打破
}
//下面是选择当前更好结果的代码
如果(total1+total2+total3=基金-限额){
双电流成本=总计1+总计2+总计3;
如果(当前成本>最大成本){
最大成本=当前成本;
amountOfMaxCostOptional=可选。of(新金额(amt1、amt2、amt3));
}
}
以下是全部代码:

import java.text.DecimalFormat;
import java.util.Optional;
import java.util.Scanner;

public class GrandFund {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        DecimalFormat fmt = new DecimalFormat("#.##");

        int amt1 = 0, amt2 = 0, amt3 = 0;
        double price1, price2, price3, fund, limit, total1 = 0, total2 = 0, total3 = 0;

        System.out.println("Equipment #1 Price:");
        price1 = scan.nextDouble();

        System.out.println("Equipment #2 Price:");
        price2 = scan.nextDouble();

        System.out.println("Equipment #3 Price:");
        price3 = scan.nextDouble();

        System.out.println("Total Fund Amount:");
        fund = scan.nextDouble();

        System.out.println("Remaining Fund Limit:");
        limit = scan.nextDouble();

        double maxCost = 0d;
        Optional<Amount> amountOfMaxCostOptional = Optional.empty();
        for (amt1 = 0; price1 * amt1 <= fund; amt1++) {
            total1 = price1 * amt1;

            for (amt2 = 0; price2 * amt2 <= fund - total1; amt2++) {
                total2 = price2 * amt2;

                for (amt3 = 0; price3 * amt3 <= fund - total1 - total2; amt3++) {
                    total3 = price3 * amt3;

                    if (total1 + total2 + total3 > fund) {
                        break;
                    }
                    //here is the code to pick the current better result
                    if (total1 + total2 + total3 <= fund
                            && total1 + total2 + total3 >= fund - limit) {
                        double currentCost = total1 + total2 + total3;
                        if (currentCost > maxCost) {
                            maxCost = currentCost;
                            amountOfMaxCostOptional = Optional.of(new Amount(amt1, amt2, amt3));
                        }
                    }
                }
            }
        }

        amountOfMaxCostOptional.ifPresent(amountOfMaxCost -> {
            double remainder =
                    fund - (amountOfMaxCost.getAmt1() * price1 + amountOfMaxCost.getAmt2() * price2
                            + amountOfMaxCost.getAmt3() * price3);
            System.out.println(
                    fmt.format(remainder) + ", " + fmt.format(amountOfMaxCost.getAmt1()) + ", "
                            + fmt.format(amountOfMaxCost.getAmt2()) + ", " + fmt
                            .format(amountOfMaxCost.getAmt3()));
        });
        scan.close();
    }

    private static class Amount {
        private double amt1;
        private double amt2;
        private double amt3;

        public Amount(double amt1, double amt2, double amt3) {
            this.amt1 = amt1;
            this.amt2 = amt2;
            this.amt3 = amt3;
        }

        public double getAmt1() {
            return amt1;
        }

        public double getAmt2() {
            return amt2;
        }

        public double getAmt3() {
            return amt3;
        }
    }
}
导入java.text.DecimalFormat;
导入java.util.Optional;
导入java.util.Scanner;
公营级公积金{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
DecimalFormat fmt=新的DecimalFormat(“#.##”);
int amt1=0,amt2=0,amt3=0;
双倍价格1,价格2,价格3,基金,限额,总计1=0,总计2=0,总计3=0;
系统输出打印项次(“设备1价格:”;
price1=scan.nextDouble();
系统输出打印项次(“设备2价格:”;
price2=scan.nextDouble();
系统输出打印项次(“设备3价格:”;
price3=scan.nextDouble();
System.out.println(“资金总额:”);
fund=scan.nextDouble();
System.out.println(“剩余资金限额”);
limit=scan.nextDouble();
双倍最大成本=0d;
可选amountOfMaxCostOptional=可选。空();
对于(amt1=0;价格1*amt1{
双倍余数=
基金-(amountOfMaxCost.getAmt1()*价格1+amountOfMaxCost.getAmt2()*价格2
+amountOfMaxCost.getAmt3()*price3);
System.out.println(
fmt.format(余数)+“,“+fmt.format(amountOfMaxCost.getAmt1())+”,”
+format(amountOfMaxCost.getAmt2())+“,”+fmt
.format(amountOfMaxCost.getAmt3());
});
scan.close();
}
私有静态类数量{
私人双amt1;
私人双amt2;
私人双amt3;
公共金额(双倍amt1、双倍amt2、双倍amt3){
this.amt1=amt1;
this.amt2=amt2;
this.amt3=amt3;
}
公共双getAmt1(){
返回amt1;
}
公共双getAmt2(){
返回amt2;
}
公共双getAmt3(){
返回amt3;
}
}
}
“不工作”不是工作问题描述。请阅读并相应地强化您的问题。