0

所以我有一个充满不同伏特加酒的字符串数组,我想做的是为数组中的每个伏特加酒(字符串)附加一个价格,这样我就可以在屏幕上显示伏特加酒及其价格,我也想用这个根据用户输入过滤出价格较高或价格较低的伏特加。这是一个示例数组:

    public static final String[] Vodka = {"Absolut Vodka","Finlandia","Ketel One","Polmos Krakow","Skyy","Smirnoff Vodka",
    "Stolichnaya","Fleischmann's","Gilbey's","Wolfschmitt","Five-O-Clock", "Grey Goose",};

提前谢谢大家!

==================================

这是我正在尝试的一些代码,感谢迈克!

if(Price != 0){ 
    for (com.famousmods.what.should.i.drink.VodkaList.Vodka vodka : vl.vodkaList){ 
        // Set vodka brand final 
        TextView text21 = (TextView) findViewById(R.id.display2); 
        test = random.next(vodka.getPrice() <= Price); 
        text21.setText(test); 
    } 
}

价格是用户输入的最大金额,它是双倍的,我试图用那段代码做的是随机获得低于输入价格的饮料。(真的只是在黑暗中拍摄)

4

4 回答 4

3

只需创建一个具有 2 个字段的自定义类“Vodka”:名称和价格。然后制作一个封装“Vodka”类的“VodkaList”类,并包含一个ArrayList<Vodka> 这样可以使一切井井有条。

因此,例如:

import java.util.ArrayList;

public class VodkaList {

    public class Vodka {

        String name;
        double price; 

        public Vodka(String name, double price) {
            this.name = name;
            this.price = price;
        }

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public double getPrice() {
            return this.price;
        }

        public void setPrice(double price) {
            this.price = price;
        }


    }

    public ArrayList<Vodka> vodkaList;

    public VodkaList() {
        this.vodkaList = new ArrayList<Vodka>();

        // here's where you can hard-code the list of Vodkas
        vodkaList.add(new Vodka("Absolut Vodka", 15.75));
        vodkaList.add(new Vodka("Findlandia", 10.25));
        // and repeat until you've hard-coded them all
    }


}

通过使用自定义类,您可以随时更改伏特加的名称/价格,无需担心跟踪数组索引,并轻松搜索列表中您想要的名称/价格。

以下是您将在主要活动中放入的用于初始化 VodkaList 的内容:

VodkaList vl = new VodkaList();

想要遍历列表并查看您放入了哪些伏特加酒?

for (Vodka vodka : vl.vodkaList)
    Log.i("Vodka", "Name = " + vodka.name + ", Price = " + vodka.price);

让我们探索一个示例场景(以解决您的问题陈述中的问题)。假设用户输入他/她将支付的最高价格“10”。

for (Vodka vodka : vl.vodkaList) {
    if (vodka.getPrice() < 10)
        ; // the price is good! the user wants it.  show them it
    else
        ; // too expensive for the user.. don't show it
}

本课程将使此类活动变得容易!

告诉我这是否有效。如果没有,我会提供更多建议。

编辑:

    Random random = new Random();
    boolean available = false;

    for (Vodka v : vodkaList) {
        if (v.price <= Price)
            available = true;
    }

    TextView text21 = (TextView) findViewById(R.id.display2);

    if (available) {
        // There exists at least one Vodka lower than the user's price
        int randomIndex = -1;
        while (true) {
            randomIndex = random.nextInt(vodkaList.size());
            Vodka v = vodkaList.get(randomIndex);
            if (v.price <= Price) {
                // We have a match!  Display it to the user
                text21.setText(v.name);
                break;
            }
            // If we got here, there's no match.. loop again!

        }
    } else {
        // No vodka exists unders the users price! Can't display anything
    }
于 2012-08-29T17:28:08.163 回答
2
public class Vodka
{
  private String name;
  private int price;

  public Vodka() {
    super();
    // TODO Auto-generated constructor stub
   }
   public void setName(String name)
   {
     this.name=name;
   }
   public void setPrice(int price)
   {
    this.price=price;
   }

   public String getName()
   {
    return this.name;
   }
   public int getPrice()
   {
    return this.price;
   }

}

在你的活动中,

List<Vodka> myList = new ArrayList<Vodka> ();
      Vodka vodk = new Vodka();
      vodk.setName("Smirnoff");
      vodk.setPrice(10);

      myList.add(vodk);

要检索数据:

String name = myList.get(0).getName();
 int price = myList.get(0).getPrice();
于 2012-08-29T17:31:43.790 回答
2

您需要改用二维数组:

public static final String[][] drinks= { 
          {"Absolut Vodka", "10.0"}, {"Finlandia", "15.0"}
          {"Ketel One", "10.0"}, {"Stolichnaya", "15.0"}

}
于 2012-08-29T17:28:33.777 回答
0

使用一些OOP 概念,Collections类也会对你有很大帮助。

package com.mac.demo;

import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

public class SortBean {
public String name;
public double price;

SortBean(String name, Double price) {
    this.name = name;
    this.price = price;
}

public static void main(String[] args) {

    SortBean sb1 = new SortBean("Absolut Vodka", 20.0);
    SortBean sb2 = new SortBean("Finlandia", 30.0);
    SortBean sb3 = new SortBean("Ketel One", 80.0);

    SortedMap<Double, SortBean> sMap = new TreeMap<Double, SortBean>();
     // IF YOU WANT TO SORT IN REVERSE ORDER 
    //SortedMap<Double, SortBean> sMap = new TreeMap<Double, SortBean>(Collections.reverseOrder());

    sMap.put(sb1.price, sb1);
    sMap.put(sb2.price, sb2);
    sMap.put(sb3.price, sb3);

    for (Entry<Double, SortBean> obj : sMap.entrySet()) {
        System.out.println(obj.getKey());
    }

    }
}

输出:

20.0
30.0
80.0
于 2012-08-29T17:29:12.517 回答