Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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_Iteration - Fatal编程技术网

在Java中,当整数有一个最大值时,如何有效地使用字符串、整数对的每个组合进行计算?

在Java中,当整数有一个最大值时,如何有效地使用字符串、整数对的每个组合进行计算?,java,iteration,Java,Iteration,假设你有三件物品:一支钢笔、一个盒子和一辆汽车。您最多可以有9支钢笔、5个盒子和2辆汽车。你如何计算你可能拥有的每一项组合的总价值 以下是我目前拥有的代码: int maxPens = 9; int maxBoxes = 5; int maxCars = 2; Double penPrice = 1.5; Double boxPrice = 50.0; Double carPrice = 150.0

假设你有三件物品:一支钢笔、一个盒子和一辆汽车。您最多可以有9支钢笔、5个盒子和2辆汽车。你如何计算你可能拥有的每一项组合的总价值

以下是我目前拥有的代码:

        int maxPens = 9;
        int maxBoxes = 5;
        int maxCars = 2;

        Double penPrice = 1.5;
        Double boxPrice = 50.0;
        Double carPrice = 150.0;

        int penCount = 0;
        int boxCount = 0;
        int carCount = 0;
        int totalCount = 1;

        while (penCount <= maxPens) {
            boxCount = 0;
            while (boxCount <= maxBoxes) {
                carCount = 0;
                while (carCount <= maxCars) {
                    Double totalPrice = (penCount * penPrice) + (boxCount * boxPrice) + (carCount * carPrice);
                    System.out.println(totalCount + " = " + totalPrice);

                    totalCount++;

                    carCount++;
                }
                boxCount++;
            }
            penCount++;
        }
int-maxPens=9;
int-maxbox=5;
int maxCars=2;
双笔价格=1.5;
双箱价格=50.0;
双心皮=150.0;
int penCount=0;
int-boxCount=0;
int carCount=0;
int totalCount=1;

while(penCount您可以遍历Map,但我认为最干净的解决方案是创建一个新的类项:

public class Item {
   String name;
   int max;
   double price;

   Item(String name, int max, double price) {
       this.name = name;   
       this.max = max
       this.price = price;    
   }

} 
这个类应该包含getter和setter

并使用for循环代替while:

Item pen = new Item(Pen, 5, 1.5)
Item box = new Item(Box, 5, 50.0)
Item car = new Item(Car, 2, 150.0)

for (int i; i<=pen.getMax; i++){
  for (int j; j<=box.getMax; j++){
    for (int k; k<=car.getMax; k++){
      Double totalPrice = (i * pen.getPrice()) + (j * box.getPrice()) + (k * car.getPrice());
      int total = i+j+k;
      System.out.println(total + " = " + totalPrice);
    }
  }
}
项目笔=新项目(笔,5,1.5)
项目框=新项目(框,5,50.0)
项目car=新项目(car,2150.0)

对于(inti;我不是一个漂亮的解决方案,但我认为类似的解决方案足够通用。你是对的,它不漂亮!但它确实似乎勾选了框。肯定比我的更干净,所以谢谢你!仍然无法解决在不必更改代码的情况下添加另一项的问题。例如,如果我添加了“cup”项,我将添加另一个for循环以包装现有的3个for循环。
Item pen = new Item(Pen, 5, 1.5)
Item box = new Item(Box, 5, 50.0)
Item car = new Item(Car, 2, 150.0)

for (int i; i<=pen.getMax; i++){
  for (int j; j<=box.getMax; j++){
    for (int k; k<=car.getMax; k++){
      Double totalPrice = (i * pen.getPrice()) + (j * box.getPrice()) + (k * car.getPrice());
      int total = i+j+k;
      System.out.println(total + " = " + totalPrice);
    }
  }
}