列出所有小于或等于X的变量

时间:2018-02-02 11:40:05

标签: java

我想列出水,爆米花,糖果,果汁等变量。我在此列表中有大约30个变量。我需要设置一种方式,在用户输入其总货币后,它将列出他们可以购买的每件商品的数量。例如:

int money = user.nextint();
System.out.println("With " + money + " dollars you can buy" +
                   "# of Popcorn or # number of candy or # of juice etc");

多维数组是最好的方法吗?我将在下面添加一些想法

System.out.println("You can buy " + item1/Money + " popcorn.");

我想我可以手动添加每个项目并将其成本除以金钱。然而,对于30多个项目来说,这显然是非常繁琐的,如果有更有效的方法,我只是好奇。

此外,我不想包含不实惠的物品。我想从println中排除这些内容。我唯一的理论包括很多ifif-else陈述。

4 个答案:

答案 0 :(得分:0)

public class Main {
   public static  class Item {
      private double price;
      private String itemName;
      //  getter setter
   }

   public static void list( List<Item> availableItems, double money ) {
     for( Item item : availableItems ) {
         if( item.getPrice() <= money ) {
            System.out.println("You can buy " + ( int ) ( money / item.getPrice() ) + " " + item.getItemName() );
         }
     }

     // java 8 code is as below

     // availableItems.forEach( item -> { if( item.getPrice() <= money ) {
     //       System.out.println("You can buy " + ( int ) ( money / item.getPrice() ) + " " + item.getItemName() );
     //    } } );

   public static void main(String[] str ) {

       List<Item> availableItems = null;
       double money;
       // to do code for item initialization
       // get user input for money
      list ( availableItems, money );
   }
}

答案 1 :(得分:0)

写一个包含项目及其价格的课程。维护一个项目列表,然后迭代它们并计算每个项目的数量。

class Item
{
    public final String name;
    public final int price;

    public Item(final String name, final int price)
    {
        this.name = name;
        this.price = price;
    }

    public int getQty(final int budget)
    {
        return budget / price;
    }
}

class Main
{
    public static void main(String... args)
    {
        final List<Item> items = Arrays.asList(
            new Item("Popcorn", 10_000), // always a rip-off
            new Item("Water", 1),
            new Item("Juice", 3)
        );

        final int money = 10;

        for (final Item item : items)
        {
            final int qty = item.getQty(money);
            if (qty > 0) // exclude things we can't buy
            {
                System.out.println("You can buy " + qty + " " + item.name);
            }
        }
    }
}

答案 2 :(得分:0)

这里有一些可以帮助你的代码。

项目类

public class Item {

  private String name;
  private double price;

  public Item(String name, double price) {

    this.name = name;
    this.price = price;
  }

  public String getName() {

    return name;
  }

  public double getPrice() {

    return price;
  }
}

主要

Item[] items = new Item[]{
  new Item("Popcorn", 2.5),
  new Item("Water", 1.0),
  new Item("Candy", 3.0),
  new Item("DVD", 14.5),
  ...
};

double credit = input.nextDouble();

System.out.println("Credit: " + credit + "$");

for(Item item : items) {

  if(credit > item.getPrice()) System.out.println((int) (credit / item.getPrice()) + "x " + item.getName() + "(" + item.getPrice() + "$) -> " + (credit % item.getPrice()) + "$");
}

输出

// For example    

Credit: 12.5$
5x Popcorn(2.5$) -> 0.0$
12x Water(1.0$) -> 0.5$
4x Candy(3.0$) -> 0.5$

不会发行非经济实惠的物品

答案 3 :(得分:0)

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class MapTest {
    public static void main(String[] args) {
        int totalCurrency = 30;
        HashMap<String, Integer> itemsMap = new HashMap<>();
        itemsMap.put("Water", 30);
        itemsMap.put("Popcorn", 10);
        Set<Entry<String, Integer>> itemSet = itemsMap.entrySet();
        String output = "With " + totalCurrency + " dollars you can buy ";
        for (Entry entry : itemSet) {
            if ((Integer) entry.getValue() <= totalCurrency) {
                int temp = totalCurrency / (Integer) entry.getValue();
                output += temp + " of " + entry.getKey() + " ";
            }
        }
        System.out.println(output);
    }
}