所以我有一个充满不同伏特加的字符串数组,我想要做的是为数组中的每个伏特加(字符串)附加价格,以便我可以在屏幕上显示伏特加及其价格,我也想要使用它来过滤掉更高价格或更低价格的伏特加,具体取决于用户输入。 这是一个示例数组:
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);
}
}
价格是用户输入的最大金额,它是一个双倍,我试图用这段代码随机获得的饮料低于输入的价格。 (这真的只是在黑暗中拍摄)
答案 0 :(得分:3)
只需制作一个自定义课程&#34; Vodka&#34;有2个字段:名称和价格。然后制作一个&#34; VodkaList&#34;封装了&#34;伏特加&#34; class并包含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);
让我们探索一个示例场景(解决问题陈述中的问题)。让我们说用户输入&#34; 10&#34;以他/她将支付的最高价格。
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
}
答案 1 :(得分:2)
您需要使用二维数组:
public static final String[][] drinks= {
{"Absolut Vodka", "10.0"}, {"Finlandia", "15.0"}
{"Ketel One", "10.0"}, {"Stolichnaya", "15.0"}
}
答案 2 :(得分: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();
答案 3 :(得分:0)
使用一些 OOP概念,集合类也会对您有所帮助。
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